forvas
forvas

Reputation: 10189

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.

Upvotes: 2

Views: 325

Answers (3)

anubhava
anubhava

Reputation: 785196

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]

Upvotes: 1

clami219
clami219

Reputation: 3038

The problem is probably in the regular expression. Try with this one:

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

Upvotes: 0

Jon Lin
Jon Lin

Reputation: 143896

Try getting rid of the leading slash in the pattern:

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

URI's that are sent through rules in the htaccess files have the leading slash stripped off so the pattern needs to omit it.

Upvotes: 0

Related Questions