I want to make my url go from
root.com/sub/?page=home
TO
root.com/home
I want to remove the subfolder, remove the php and just leave the most basic url. Also if possible i want to redirect all non 'root.com/[*]' back to root.com. For example 'root.com/home/index.php?page=home' and 'root.com/sub/test/' would both redirect back to 'root.com'.
.htaccess and mod_rewrite seems to be the best way So far i have this:
RewriteEngine On
#remov开发者_开发问答e subfolder, WORKS
RewriteCond %{HTTP_HOST} ^root\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.root\.com$
RewriteCond %{REQUEST_URI} !^/sub/
RewriteRule (.*) /sub/$1 [L]
This works and removed the subfolder
#remove PHP, not working :S
RewriteRule ^([a-zA-Z0-9_-]+)$ /index.php?page=$1 [L]
RewriteRule ^([a-zA-Z0-9_-]+)/$ /index.php?page=$1 [L]
But this is not working and leaves the '?page=home' ect I think im missing something before the regex for the second rule, but im new to this.
Thanks in advance!
EDIT: taken into account the [L] thing, still no dice.
I'm guessing it needs to be done in one hit rather then seperate iterations.
The problem of mod_write
usually lies on conflicts; in this case, the second block will be matched by the first block append afterword, creating an infinite loop or 500 error.
You should append the flag [L]
to every RewriteRule
. E.g.
RewriteEngine On
#remove subfolder
RewriteCond %{HTTP_HOST} ^root\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.root\.com$
RewriteCond %{REQUEST_URI} !^/sub/
RewriteRule (.*) /sub/$1 [L]
#remove PHP
RewriteRule ^([a-zA-Z0-9_-]+)$ /index.php?page=$1 [L]
RewriteRule ^([a-zA-Z0-9_-]+)/$ /index.php?page=$1 [L]
Without [L]
, /abc
will be rewrite to /index.php?page=abc
, and that will rewrite to /sub//index.php?page=abc
.
I am 75% sure this is the problem, but it's untested so I am not going to bet on it. Never hurt trying though.
This rule is the problem I think:
RewriteRule (.*) /sub/$1 [L]
Try changing it to:
RewriteRule (.*) /sub/index.php?page=$1 [L]
精彩评论