开发者

How to make a virtual folder in .htaccess

I got this code for usernames like mysite.com/username

Options +FollowSymLinks

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$ profile.php?user=$1
  • My users can submit posts. And my file for posts is topic.php which is also in root directory. I开发者_开发问答f I rewrite topic.php, code thinks that it's a username. I need a folder like mysite.com/watch/topic.php but I want to make it like folder doesn't exists (virtualy) but I can access the topics through that URL.

Sorry for my bad english. thank you.


Try this:

Options +FollowSymLinks
RewriteEngine On

RewriteRule ^watch/(.+)/?$ $1 [NC,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ profile.php?user=$1

It will rewrite all requests for files in your 'virtual directory' (watch) to files in your root folder. The 'L' behind the rule makes sure it will be the last rewrite for the request.

If the requested page is not in the 'virtual directory', it will assume the user is requesting a user profile, so it will rewrite the request to the user profile you specified

On a side note: I think you should change the profile request to something like

RewriteRule ^user/([a-z0-9-_]+)/?$ profile.php?user=$1 [NC,L]

Maybe now you think this is not neccesary, but in the future when you are trying to implement more rewrites, this enables you to distinguish what the user is actually requesting.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜