开发者

Specific php5 mod_rewrite rule to process a user-entered short url server side?

I browsed the plethora of answers here but couldn't find one specific enough to mimic for my needs. I'm not looking to create a shortened URL, I'm looking for a way for the server to detect the format of a shortened URL, expand it and display the page normally (and of course without changing the look of the shortened URL in the actual browser URL bar). Here's what I'd like to do...

I have the following url structure:

http://www.mysite.com/members.php?user_name

I'd like users to access these dynamic pages using shortened URLs. They should be able to access this same page by typing in:

http://www.mysite.com/users/user_name

..and the mod_rewrite rule would know to parse it as the full url above. Any ideas how this would be w开发者_如何学编程ritten? Notice the parameter is not passed with a value.

PS: Not looking to mess with any of the application code or parameters, just looking for a rule to simply read the short URL and covert server-side, if at all possible. Thanks!


Try something like

RewriteCond %{REQUEST_FILENAME} !-s
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/users/(.*) /members.php?$1 [l]

This makes the assumption that any url that doesn't map to an existing directory or file should be considered a user_name.

EDIT: Changed rule to match updated question. You should really read the mod_rewrite documentation. Also based on the updated URL the two RewriteCond may not be necessary.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜