2

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.

forvas
  • 9,801
  • 7
  • 62
  • 158

3 Answers3

1

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]
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • You are right, I have mod_rewrite enabled but my .htaccess is not working. Where do I have to write AllowOverride All? In apache2.conf? – forvas Jan 15 '14 at 17:53
  • Yes that is done in apache2.conf in the `` section. This will require apache restart also. – anubhava Jan 15 '14 at 17:55
  • Now I have this on that apache2.conf section: ` Options Indexes FollowSymLinks AllowOverride All Require all granted ` – forvas Jan 15 '14 at 17:59
  • ya looks good, restart Apache and rerun that .htaccess verification. – anubhava Jan 15 '14 at 18:15
0

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.

Jon Lin
  • 142,182
  • 29
  • 220
  • 220
0

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]
clami219
  • 2,958
  • 1
  • 31
  • 45
  • Thank you, I pasted your code, but still same problem: Not Found The requested URL /en/ was not found on this server. – forvas Jan 15 '14 at 16:21