开发者

htaccess not working as it should

开发者 https://www.devze.com 2022-12-19 14:16 出处:网络
well, ofcourse its not working, im still a n00b :) this is the code that i have : Options +FollowSymLinks

well, ofcourse its not working, im still a n00b :)

this is the code that i have :

Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*)\.css - [S=7]
RewriteRule (.*)\.jpg - [S=6]
RewriteRule ^gallery gallery.html [L]

RewriteRule (.*)/(.*)/ index.html?page=$2 [L]
RewriteRule (.*)/(.*) index.html?page=$2 [L]
RewriteRule (.*)/ index.html?page=$1 [L]
RewriteRule (.*) index.html?page=$1 [L,QSA]

Now, this works fine, if i try localhost/abc but it wont work if i try localhost/abc/

also, when i try localhost/abc/def (or localhost/abc/def/ for that matter) the css file isnt being included properly. i get an error saying 开发者_C百科/abc/def/style.css does not exist. :(

however, the code doesnt work if i remove the QSA flag from the last rule. this is the code that ive come up with after a lot of googling and reading SO. if anyone can help out i'll be extremely grateful.

thanks!


Sort your trailing slash issue with (/)? rather than / alone

?=zero or one of the preceding char


You should use more specific patterns. .* means zero or more arbitrary characters. That matches anything, even the empty string.

In your case abc/ is probably matched by the rule with the pattern (.*)/(.*) and not the rule with (.*)/ as you might have assumed.

Try [^/]+ instead (one or more characters except the /) and add assertions for the start (^) and end of the string ($):

RewriteRule ^([^/]+)/([^/]+)/$ index.html?page=$2 [L]
RewriteRule ^([^/]+)/([^/]+)$ index.html?page=$2 [L]
RewriteRule ^([^/]+)/$ index.html?page=$1 [L]
RewriteRule ^([^/]+)$ index.html?page=$1 [L,QSA]

And as shaunhare.co.uk already said, you can set the trailing / optional by appending the ? quantifier to it:

RewriteRule ^([^/]+)/([^/]+)/?$ index.html?page=$2 [L]
RewriteRule ^([^/]+)/?$ index.html?page=$1 [L]
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号