简体   繁体   中英

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 written? 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.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM