开发者

RewriteRule to redirect tp a page for a second subfolder variable

开发者 https://www.devze.com 2022-12-16 22:50 出处:网络
FTR, I\'m pretty abysmal at rewrite rules. We\'ve got a number of users who each have their own projects. Right now we\'ve got the RewriteRules set up so that when someone just types mysite.com/user

FTR, I'm pretty abysmal at rewrite rules.

We've got a number of users who each have their own projects. Right now we've got the RewriteRules set up so that when someone just types mysite.com/username it goes to the user's profile at, f.e., mysite.com/work/user_name.php?u=000

But what we really need is, when someone types in mysite.com/username/project1 it goes to that project. (we don't have to do anything with the user, really. We just need to find the project. But the username needs to be in there for seo/sanity purposes) I've tried to accomplish this for two days now and I can't seem to get anything but 500 errors no matter what I do, and I'm simply not talented enough to figure it out.

I originally tried to redirect to the user page with a second variable ($2) and if that var was there, do a php header to the appropriate page, but that didn't seem to work and it's still showing the URL as mysite.com/work/project_page?i=999 or whatever, and that's no g开发者_运维问答ood either.

Here's the code Ive got for redirecting to the user

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /work/user_page.php?n=$1 [L,QSA]

But I can't seem to figure out how to tell it that if there's 2 vars, (i.e. user/project) then redirect to work/project_page.php?n=$2...


You need to use a more specific pattern like [^/]+ (one or more arbitrary characters except /) instead of .* (an arbitrary number of any character). This enables you to distinguish between one or more path segments:

# one path segment
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ /work/user_page.php?n=$1 [L,QSA]
# two path segments
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)$ /work/user_page.php?n=$1&project=$2 [L,QSA]
0

精彩评论

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