RewriteRule重定向到绝对路径,而不是正确的URL [英] RewriteRule is redirecting to an absolute path, rather than the proper URL

查看:399
本文介绍了RewriteRule重定向到绝对路径,而不是正确的URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为我的网站创建了一堆RewriteRules,并且在我的本地设置中没有问题.这是我的.htaccess的摘录:

I've created a bunch of RewriteRules for my website and have had no problem with them on my local setup. Here's a snippet from my .htaccess:

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteRule ^news/?$ news.php [L]

在本地,当我访问127.0.0.1/news时,它会正确地重定向到news.php并屏蔽URL.尝试访问上述示例时,我刚刚更新了远程服务器上的.htaccess文件,并收到此错误:

Locally, when I visit 127.0.0.1/news, it redirects properly to news.php and masks the URL. I just updated the .htaccess file on the remote server and receive this error, when trying to visit the above example:

在此服务器上找不到请求的URL/mnt/target02/123456/123456/www.mywebsite.com/web/content/news.php.

我尝试将规则更改为此:

I've tried changing the rule to this:

RewriteRule ^news/?$ http://www.mywebsite.com/news.php [L]

,页面将正确加载.但是,地址栏显示的是news.php,而不是新闻.有什么我想念的东西吗?还是我呆在丑陋(且不太安全)的地址上? 谢谢!

and the page loads properly. However, the address bar shows news.php, rather than news. Is there something I am missing, or am I stuck with the ugly (and less secure) address? Thanks!

推荐答案

如果您像我一样,则尝试避免在替换URL(新目标)中添加前导斜杠,以使相同的.htaccess文件可以与测试服务器和生产服务器.我遇到了同样的问题,需要做的只是设置:

If you are like me you are trying to avoid adding leading slashes to your substitution URL (the new destination) so that your same .htaccess file can work with both your test and production servers. I had the same problem you were having and all that was needed was to set:

RewriteBase /

这告诉mod_rewrite将替代URL设置为基于Web根目录而不是文件根目录.另一个提示,如果您需要重定向到目标的顶层,请使用./而不是仅使用/.我的测试服务器有一个端口(8080),并且不在根目录(因此它的非空RewriteBase/site1/),并且Web上的大多数重写规则示例都没有考虑这种情况.如果您的替换从未以斜杠开头,则可以在测试服务器和实时服务器上使用相同的.htaccess文件,而您可能需要更改的只是RewriteBase的值,而不是更新每个规则.

This tells mod_rewrite to base the substitution URLs off of the web root rather than the file root. Another tip, if you need to redirect to the top level of the destination, use ./ instead of just /. My test server has a port (8080) and is not at the root (so it has a non-empty RewriteBase of /site1/), and most of the rewrite rule examples on the web do not consider this case. If you never have your substitutions start with a leading slash, you can use the same .htaccess file on your test server and live server, and all you might need to change is the value of RewriteBase, rather than updating every single rule.

这篇关于RewriteRule重定向到绝对路径,而不是正确的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆