开发者

.htaccess profile rewrite help

开发者 https://www.devze.com 2023-01-19 13:23 出处:网络
RewriteRule ^user/(.*)/(.*) profile.php?user=$1&v=$2 [L] works for http://10.0.1.5/user/Kevin/wall/

RewriteRule ^user/(.*)/(.*) profile.php?user=$1&v=$2 [L] works for http://10.0.1.5/user/Kevin/wall/

if i do http://10.0.1.5/user/Kevin/wall it 404's. if i do http://10.0.1.5/user/Kevin/ it 404's if i do http://10.0.1.5/user/Kevin it 404's

I tired

 RewriteRule ^开发者_如何学Pythonuser/(.*) profile.php?user=$1 [L]
    RewriteRule ^user/(.*) profile.php?user=$1 [L]
    RewriteRule ^user/(.*)/(.*)/ profile.php?user=$1&v=$2 [L]
    RewriteRule ^user/(.*)/(.*) profile.php?user=$1&v=$2 [L]

But it didn't work like i wanted. I want it to work with all 4 possible urls. What should i do?


Use a more specific pattern than .* like [^/]+ and use /?$ to make the trailing slash optional:

RewriteRule ^user/([^/]+)/?$ profile.php?user=$1 [L]
RewriteRule ^user/([^/]+)/([^/]+)/?$ profile.php?user=$1&v=$2 [L]

But as I recommend to just use one of the formats (with or without trailing slash), remove or add the trailing slash if it is present or missing:

# remove trailing slash
RewriteRule (.*)/$ /$1 [L,R=301]

# add trailing slash
RewriteCond %{REQUEST_URI} !-f
RewriteCond %{REQUEST_URI} !-d
RewriteRule .*[^/]$ /$0/ [L,R=301]
0

精彩评论

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

关注公众号