I have these rules and I can't figure out why one of them won't work:
RewriteEngine On
RewriteBase /
Options +FollowSymlinks
Options -MultiViews
Options -Indexes
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^error/([^/.]+)/([^/.]+)$ /pages/error.php?$1=$2 [NC,R]
RewriteRule ^([^/.]+)$ index.php?user=$1 [NC,L]
RewriteRule ^([^/.]+)/.* index.php?$0 [PT]
RewriteRule ^error/.* error.php/?$0 [PT]
Rule 1 works, echo $_GET['user']
then I get results
Rule 2 works as well. If I do a print_r($_SERVER['REQUEST_URI'])
then I get what ever is after .com/
.
so That makes me think that its catching correctly, and if I do a as well.
So those rules seem to be working... but now if I enter
domain.com/error/access/notallowed
then I get a Not Found
error, so that means that my Rule 3 that wants to catch anything that starts with error
isn't working.
Any ideas on what may be causing this behavior?
EDIT: updated rules based on answer
RewriteRule ^([^/.]+)$ index.php?user=$1 [NC,L]
RewriteCond %{REQUEST_URI} ^/error/?
RewriteRule ^([^/.]+)/?.* index.php?$1 [PT]
RewriteRule ^(error/?.*)$ /_msp/pages/error.php/?$1
The problem is that your second RewriteRule is catching domain.com/error/access/notallowed
before apache is able to make it to the third RewriteRule. So, apache says, "I've got a rule that matches, I'm finished here," and throws away the rest of your RewriteRules. If only there were a way we could tell apache to ignore the second rule in specific cases ...
"Wait, are you saying what I think you're saying? I can tell apache to ignore that second rule?"
"Yes, that's exactly what I'm saying."
RewriteRule ^([^/.]+)$ index.php?user=$1 [NC,L]
RewriteCond %{REQUEST_URI} !^/error/?
RewriteRule ^([^/.]+)/?.* index.php?$1 [PT]
RewriteRule ^(error/.*)$ index.php?$1 [PT]
See that RewriteCond line just before your second rule? You can insert any number of conditions and they will apply to the next evaluated rule. So, in this specific RewriteCond we're telling apache that the following rule shouldn't be used if the REQUEST_URI starts with /error/
.
The rewritten URL using the above rules for the address domain.com/error/access/notallowed
will be:
index.php/?error/access/notallowed
You can adjust the regex according to your needs.
I believe if you switch the last two it should work:
RewriteRule ^error/.* error.php/?$0 [PT]
RewriteRule ^([^/.]+)/.* index.php?$0 [PT]
RewriteRule ^([^/.]+)/.* index.php?$0 [PT] rule is matching the url, I believe rewriting it and passing it through as the index.php, but I am not a htaccess expert.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.