Search code examples
phpapache.htaccessmod-rewriteclean-urls

Pass a clean parameter through URL


I'm using localhost, and in my index.php page I have this code:

<? echo 'LANG IS '.$_GET['lang']; ?>

When I type localhost on the URL it only shows LANG IS, obviously, but if I type localhost/en I see a 404 Not Found message. I have to type localhost?lang=en to show my index.php code. I want to type localhost/en instead of localhost?lang=en and get the same result.

I'm using Apache2 and I have mod_rewrite enabled. I also have a .htaccess file with this code (I have changed and tested it a lot of times):

RewriteEngine on
RewriteRule ^/([a-zA-Z0-9]+|)/?$ index.php?lang=$1 [L,QSA]

I have been reading about .htaccess and clean urls for days but I couldn't make this work. Any ideas? Thank you so much in advance.


Solution

  • Most likely your .htaccess isn't even enabled. Verify it first

    To check if your .htaccess is enabled try putting same random/garbage text on top of your .htaccess and see if it generates 500 (internal server) error or not?

    It it is not enabled then then you will need AllowOverride All line in <Directory "/var/www/>` section.

    Once it is enabled following rule should work for you:

    RewriteEngine on
    RewriteRule ^(\w+)/?$ index.php?lang=$1 [L,QSA]