当前位置: 动力学知识库 > 问答 > 编程问答 >

.htaccess - Virtual directory redirects

问题描述:

I'm working on a linking system that requires use of virtual directories. I've managed to set a system up that allows users to visit domain.com/SjkpP/ which gives the same effect as domain.com/link.php?link=SjkpP although the trailing slash is necessary and when a visiting domain.com/SjkpP a 404 error occurs. I'm pretty bad as for editing and adding to htaccess files so I was wondering what could I add to allow visiting the link without a trailing slash to work?

Current .htaccess

RewriteEngine on

RewriteRule ([A-Za-z0-9]+)/ link.php?link=$1

网友答案:

If you want to make the trailing slash optional, you can use this instead:

RewriteRule ^([A-Za-z0-9]+)/?$ link.php?link=$1

This makes the final slash optional (with the regex ? zero-or-one match quantifier) but does not capture the slash if it does appear (because the slash appears outside the parentheses which capture the virtual directory name).

Just be aware that, because you're using a silent redirect, the visitor's web browser will think it's in a directory which is not the public root directory. So any relative paths (which do not begin with a forward-slash) pointing to page decoration (such as image, CSS or JavaScript files) will lead the browser to try the non-existent sub-directory and come up with nothing. However, your current fix is causing the browser to think it's in a sub-directory, so if you're not having any trouble so far then replacing your rules with this one should cause no new problems.

分享给朋友:
您可能感兴趣的文章:
随机阅读: