10

I'm trying to build a url shortener, and I want to be able to take any characters immediately after the domain and have them passed as a variable url. So for example

would become

Here's what I have for mod_rewrite right now, but I keep getting a 400 Bad Request:

RewriteEngine on  
RewriteCond %{REQUEST_FILENAME} !-f  
RewriteCond %{REQUEST_FILENAME} !-d  
RewriteRule ^(.*) index.php?url=$1 [L,QSA]  
Amit Verma
  • 40,709
  • 21
  • 93
  • 115
bswinnerton
  • 4,533
  • 8
  • 41
  • 56

6 Answers6

16

Try replacing ^(.*) with ^(.*)$

RewriteRule ^(.*)$ index.php?url=$1 [L,QSA]

Edit: Try replacing index.php with /index.php

RewriteRule ^(.*)$ /index.php?url=$1 [L,QSA]
gen_Eric
  • 223,194
  • 41
  • 299
  • 337
3

ByTheWay don't forget to enable mod rewrite,in apache on ubuntu

sudo a2enmod rewrite 

and then restart apache2

sudo /etc/init.d/apache2 restart

edit: it is an old question that helped me but I lost hours testing my apache2.conf file on one side and .htaccess file on the other side and still no light.

nassim
  • 388
  • 3
  • 9
2

To rewrite all requests to /index.php ,you can use :

RewriteEngine on


RewriteCond %{REQUEST_URI} !^/index.php$
RewriteRule ^(.+)$ /index.php?url=$1 [NC,L]

The RewriteCondition RewriteCond %{REQUEST_URI} !^/index.php$ is important as it excludes the rewrite destination we are rewriting to and prevents infinite looping error.

Amit Verma
  • 40,709
  • 21
  • 93
  • 115
0

Check under Apache modules and make sure that rewrite-module is enabled. I had a similar issue and it was resolved by enabling the rewrite-module.

  • what use does it have to answer a super old question **that already has been answered**? – Olle Kelderman Oct 31 '15 at 04:51
  • Actually this solved my problem after 2 hours of searching, I'm not a PHP programmer but I use it some times on my website. – Andre Sep 27 '19 at 19:19
0

in the framework folder create an .htaccess file with the following contents

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteRule ^$ public/ [L]
    RewriteRule (.*) public/$1 [L]
</IfModule>

then in the framework/public folder create an .htaccess file with the contents

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?url=$1 [L]
</IfModule>

by adding the .htaccess file as above we can make the URL on the website more user-friendly. For example if we write a URL like

http://google.com/asdf

will be converted to

http://google.com?url=asdf

for test purposes, we create a php index.php file in a public folder with the following content

<?php
    echo $_GET['url'];
?>

if when run in a browser using the URL above the result is 404 not found, most likely mod_rewrite has not been enabled as in step 1. if the result is 500 Internal Server Error, there may be an error in writing .htaccess

Riki krismawan
  • 503
  • 1
  • 3
  • 10
0

Try it without using using RewriteCond.

durron597
  • 31,968
  • 17
  • 99
  • 158
PseudoNinja
  • 2,846
  • 1
  • 27
  • 37
  • 2
    Those two `RewriteCond` lines are to make sure that files and folders that really exist don't get rewritten. For example `http://example.com/images/logo.png` won't get rewritten if that image exists. – gen_Eric Dec 21 '11 at 20:38
  • @Rocket Good to know for future reference. I was just working with the information I was given. – PseudoNinja Dec 21 '11 at 20:40