Reputation: 877
i'm trying to rewrite some simple urls:
http://www.site.com/folder/index.php?page=something
into
http://www.site.com/folder/something
here's my .htaccess file ( placed in /folder/ )
RewriteEngine On
RewriteBase /folder
RewriteRule ^(.*)$ index.php?page=$1 [R]
with this rule i always get this URL
http://www.site.com/folder/index.php?page=index.php
what am i doing wrong?
Upvotes: 0
Views: 2647
Reputation: 877
Thanks to everyone i succeeded in doing what i was thinking to do:
RewriteEngine On
RewriteBase /folder/
#Excluding direct url to php files
RewriteCond %{REQUEST_URI} !(.*)\.php$
#Excluding the empty space url
RewriteCond %{REQUEST_URI} !^(\s*)$
RewriteCond %{REQUEST_URI} !\.(gif|jpg|png|css|js|)$ [NC]
RewriteRule ^(.*)$ index.php?page=$1 [L,QSA]
Upvotes: 0
Reputation: 4221
First, change
RewriteBase /folder
to
RewriteBase /folder/
Second, change
RewriteRule ^(.*)$ index.php?page=$1 [R]
to
RewriteRule ^index\.php/(.*)$ index.php?page=$1 [L,QSA]
That should work. The QSA part is to allow any querystring in the address to be carried over to the new address; otherwise, they will be dropped.
In that case, try this
RewriteRule ^(.*)$ index.php?page=$1 [L,QSA]
similar to what you had earlier.
Unless you have other patterns, that should achieve what you need
Hope it helps.
Upvotes: 2
Reputation: 18250
Your initial rule worked perfectly. The only problem was the [R] modifier which redirected /folder/something to /folder/index.php?page=something and caused a second request.
Apply your rule on /folder/index.php?page=something and you get /folder/index.php?page=index.php
To fix this replace [R] ( HTTP 302 Redirect ) by [L] ( leave matching chain when you match here ) and you're done
Upvotes: 0
Reputation: 570
If I may suggest, please look into a generator for htaccess, such as: http://www.generateit.net/mod-rewrite/
which gives you:
RewriteEngine On
RewriteRule ^folder/([^/]*)$ /folder/index.php?page=$1 [L]
I've editted the above, this is a working example on a local testdomain. Perhaps this is just what you have been looking for.
Also, if I may, omitting the extension of a page is not recommendable. Google likes it to be recognizable e.g. .html or .php.
And, you should add a rewriterule for every parameter you add.
Finally, I would omit the index.php. Your server will automatically load index.php if no other page is found, but it doesn't have to be visible in the addressbar.
Upvotes: -1
Reputation: 16948
You are referencing the wrong match, use $2
like so
RewriteEngine On
RewriteBase /folder
RewriteRule ^(.*)$ index.php?page=$2 [R]
The above is infact incorrect. It seems that the .
is not matching the correct characters (I don't know why).
The best way I can see getting round this without knowing the cause of the issue is to simply match letters and numbers like so:
# Turn on the rewrite engine
RewriteEngine On
# Set the rewrites for /folder
RewriteBase /folder
# Set the rewrite rules
RewriteRule ^([a-z0-9]+)/?$ index.php?page=$1 [NC]
Upvotes: 1