如何使用 .htaccess 中的 rewriterules 更改 url?

发布于 2024-12-09 18:52:44 字数 816 浏览 1 评论 0原文

我正在使用这个重写规则:

RewriteRule ^page/([^/]*)$ http://example.com/page?q=$1 [QSA,L]

如果我访问 example.com/page/somePage 我会被重定向到 example.com/page?q=somePage

但我没有想要重定向,我想要的 URL 始终是 example.com/page/somePage

如何做到这一点?
谢谢,


我删除了 http://example.com 但它不起作用,我得到页面未找到。


我的网站使用 Wordpress,这是我完整的 .htaccess:


    RewriteEngine On
RewriteBase /
RewriteRule ^page/([^/]+)/?$ page?q=$1 [L]

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

I'm using this rewriterule:

RewriteRule ^page/([^/]*)$ http://example.com/page?q=$1 [QSA,L]

If I go to example.com/page/somePage I get redirected to example.com/page?q=somePage

But I don't want a redirection, what I want is the URL to always be example.com/page/somePage

How to do this?
Thank you


I removed http://example.com but it doesn't work, I get Page not Found.


I am using Wordpress for my site, this is my complete .htaccess:


    RewriteEngine On
RewriteBase /
RewriteRule ^page/([^/]+)/?$ page?q=$1 [L]

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

謌踐踏愛綪 2024-12-16 18:52:44

每当您在要重写的路径开头指定 http:// 时,Apache 将始终强制 301 重定向到新 URL,无论该 URL 是否打开是否是同一个网站。只需删除 http://example.com 部分即可解决您的问题。


至于找不到页面,是否有另一个 RewriteRule 告诉“page”被处理为“page.php”或类似的东西?您保存的 PHP 文件是否没有扩展名?


那么你的问题是你肯定需要删除 [L] 标志,因为你告诉 Apache 不要再处理该请求的 RewriteRules,所以它永远不会查看 WordPress 重写,因为该规则是已经执行了,阿帕奇被告知这应该是最终规则。我建议将 [QSA] 保留在行中,这不会影响脚本的整体结果。

Whenever you specify the http:// at the beginning of the path to be rewritten to, Apache will always force a 301 redirect to the new URL, whether the URL is on the same website or not. Simply removing the http://example.com part should fix your problem.


As for the page not found, is there another RewriteRule somewhere that tells just 'page' to be processed as 'page.php' or something of the sort? Do you have your PHP files saved without extensions?


Well then your problem is you definitely need to remove the [L] flag because you're telling Apache not to process any more RewriteRules for that request, so it never looks at the WordPress rewrites because that rule was already executed and Apache was told that should be the final rule. I would recommend leaving the [QSA] in the line though, that would not affect the overall outcome of your script.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文