Emersont1
Emersont1

Reputation: 3

url rewriting with htaccess not working

I am trying to rewrite my urls in my site so whatever is after the slash is passed as an argument (example.com/Page goes to example.com/index.php?page=Page)
here is the code that isn't working (it gives a Forbidden):

RewriteEngine On
RewriteRule   ^/(.+)/$   /index.php?page=$1   [L]

Any Help will be appreciated

Upvotes: 0

Views: 25

Answers (1)

arkascha
arkascha

Reputation: 42984

This is what I suggested in the comment to your question:

RewriteEngine On
RewriteCond   %{REQUEST_FILENAME} !-f
RewriteCond   %{REQUEST_FILENAME} !-d
RewriteCond   %{REQUEST_URI}   !^/index\.php
RewriteRule   ^(.+)$   /index.php?page=$1   [L,B]

The leading slash does not make sense in .htaccess style files, since you do not process an absolute oath in there, but a relative one. About the trailing slash: your example does not show such a slash, so why do you want to have it in the regular expression? It results in your pattern not matching anything but a request terminated by a slash. Which is not what you want.

The RewriteCond lines are there to still allow access to physical existing files and directories and to prevent an endless loop, though that should not occur with an internal-only rewriting. And you need the B flag to escape the part of the request url you want to specify as GET argument.

The last condition is actually obsolete, since obviously /index.php should be a file. I leave it in for demonstration purposes.


In general it is a very good idea to take a look at the documentation of apaches rewriting module: httpd.apache.org/docs/current/mod/mod_rewrite.html

It is well written, very precise and contains lots of really good examples. It should answer all your questions.

Upvotes: 1

Related Questions