Mod重写路径问题 [英] Mod Rewrite path issues
问题描述
我已经成功创建了重写规则来处理类似URL的
I have successfully created rewrite rules to handle URLs like
这是mod重写&正则表达式:
Here is the mod rewrite & regex:
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?([0-9]+)(?:\/)?$ /index.php?p=$1 [L]
RewriteRule ^/?([-a-zA-Z0-9_+]+)(?:\/)?$ /index.php?n=$1 [L]
RewriteRule ^/?([-a-zA-Z0-9_+]+)/([-a-zA-Z0-9_+]+)(?:\/)?$ /index.php?n=$2 [L]
这很好用,但是问题是当我转到 http://example.com/xyz/abc ,我的脚本获得了正确的输入(n = abc),但是路径被弄乱了,所以我代码中的所有相对URL都被破坏了,因为它们现在相对于xyz而不是根.
This works great, but the problem is that when I go to http://example.com/xyz/abc, my script gets the correct input (n=abc) but the paths are messed up, so all relative URLs in my code are broken, as they are now relative to xyz instead of the root.
有没有办法解决这个问题?如您在上面看到的,我尝试重定向到/index.php以强制路径正确.经过一整天的正则表达式和代码,我的大脑被炸了,所以我希望这不是灾难性的琐碎事情.
Is there a way around this at all? As you can see above I have tried to redirect to /index.php to force the path to be correct. My brain is fried after a long day of regex and code, so I hope it's not something disastrously trivial.
推荐答案
这三个选项是:
1)使用基本标记(这会影响页面上的每个相对URI,包括链接,图像,脚本,样式表等)
1) Use a base tag (this will affect every relative URI on your page, including links, images, scripts, stylesheets and so on)
<base href="http://yoursite/">
2)将所有链接更改为完全限定的URI
2) Change all of your links to fully qualified URIs
<a href="http://yoursite/yourpage.html">
3)使用"/"前缀显示路径相对于每个URI的根.
3) Use the "/" prefix to show that the path is relative to the root on each URI.
<a href="/yourpage.html">
我个人最常使用base-tag选项,但确实会受到一些负面报道(使用它的人对此并不真正了解).当涉及到mod_rewrite时,基本标记是完美的,因为您可能希望所有路径都相对于根目录,包括所有图像,css,脚本和链接.
I have personally used the base-tag option the most, which does get some bad press (from people that have used it without really understanding it). When it comes to mod_rewrite, the base tag is perfect as you probably DO want all your paths to be relative to the root, including all your images, css, scripts and links.
这篇关于Mod重写路径问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!