英文:
Apache rewriterule assistance needed
问题
I am trying to remove query string: ?id= from this URL:
and keep the page loading/working fine
I tried this:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(.)?id=(.)?$ [NC]
RewriteRule ^(.*)?$ $1%2? [R=301,L]
it seems to strip the ?id= out fine but the page won't load, just 404s.
My issue is making the content load / carry over on the new URL.
Any help appreciated,
Thanks
Rob
Was expecting URL to load fine and not 404, but with the shorter URL.
英文:
I am trying to remove query string: ?id= from this URL:
and keep the page loading/working fine
I tried this:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(.)?id=(.)?$ [NC]
RewriteRule ^(.*)?$ $1%2? [R=301,L]
it seems to strip the ?id= out fine but the page won't load, just 404s.
My issue is making the content load / carry over on the new URL.
Any help appreciated,
Thanks
Rob
Was expecting URL to load fine and not 404, but with the shorter URL.
答案1
得分: 0
更新的答案:
/
丢失可能是因为您可能尚未声明 RewriteBase
。由于您正在使用整个 REQUEST_URI
,因此代码中的以下部分应该可以解决这个问题。
此外,RewriteCond
的第二个括号表达式需要一个 *
来提供完整的 QUERY_STRING
(通过搜索模式 id=/
减少)。
文件:/news-and-events/news/.htaccess
:
RewriteEngine On
RewriteBase "/news-and-events/news/"
RewriteCond %{QUERY_STRING} ^(.)?id=/(.*)?$ [NC]
RewriteRule ^(.*)?$ $1%2? [R=301,L]
简化版本
文件:/news-and-events/news/.htaccess
:
RewriteBase /news-and-events/news/
RewriteCond "%{QUERY_STRING}" "^id=/(.*)$" [NC]
RewriteRule "(.*)" "$1%1?" [R=301,L]
或者 在您的 virtualhost.conf
文件中:
这里上下文非常重要:
<VirtualHost *:80>
DocumentRoot "/var/www/vhosts/myhost/htdoc/"
# 其他指令
# 仅需要一次:
RewriteEngine On
<Directory "/var/www/vhosts/myhost/htdoc/news-and-events/news/">
RewriteBase "/news-and-events/news/"
RewriteCond "%{QUERY_STRING}" "^id=/(.*)$" [NC]
RewriteRule "(.*)" "$1%1?" [R=301,L]
</Directory>
# 更多 <Directory> 容器
</VirtualHost>
顺便说一下,将模式括在双引号中是一个好习惯,即使不总是必要的。
一些建议
您匹配的是 QUERY_STRING
:
QUERY_STRING = 'id=/nodle/nodle-io/ledger-integration-update-compatibility-status-236699676dc6'
它始终从 URL 中的 ?
后面的字符开始。
因此,在 RewriteCond
中的匹配并不是必需的。
由于正则表达式是贪婪的,您选择整个字符串,强制使用 ^
开始和 $
结束的字符串是多余的。并且通过 ?
使其变为可选是不必要的,因为 (.*)
本身可能为空。
注释
您正在匹配 QUERY_STRING
:
QUERY_STRING = 'id=/nodle/nodle-io/ledger-integration-update-compatibility-status-236699676dc6'
它始终从 URL 的 ?
后面的字符开始。
因此,在 RewriteCond
中的匹配不生效。
另外,您已经透露,代码将驻留在 Apache 服务器的 virtualhost.conf
文件中。
此外,配置不是独立的,而是 WordPress 安装的子目录,还使用了一些 Rewrite 规则。
因为您已经启用了 .htaccess
的指令,但希望将规则写在 virtualhost.conf
文件中。
使用适当的 <Directory>
容器非常重要,以防止与 WordPress 安装的交叉影响(在两个方向)。
AllowOverride All
首先清理掉可能仍然存在的以前的“实验”.htaccess
文件,以解决您的问题。非常重要!
但显然,您的意图与您提出的问题相反:
因此,除了所提出的解决方案可以实现您的要求外,显然重写后的 URL 上没有内容,您最终会得到一个 404 - 未找到
的 HTML 客户端错误。
这样做的意义在于,当由于服务软件的更改导致 URI 结构发生变化时,您希望访问者能够访问内容。此外,通过 HTML 状态码 301 - 永久移动
,搜索引擎将迅速学会使用新的 URI 逻辑并保持 SEO 。
英文:
Updated Answer:
The /
is missing because you probably have not declared the RewriteBase
. As you are using the whole REQUEST_URI
this one below in the code should do it.
Additionally the second bracket expression of the RewriteCond
needs an *
to provide the full QUERY_STRING
(reduced by the search pattern id=/
).
File: /news-and-events/news/.htaccess
:
RewriteEngine On
RewriteBase "/news-and-events/news/"
RewriteCond %{QUERY_STRING} ^(.)?id=/(.*)?$ [NC]
RewriteRule ^(.*)?$ $1%2? [R=301,L]
Simplyfied Version
File: /news-and-events/news/.htaccess
:
RewriteBase /news-and-events/news/
RewriteCond "%{QUERY_STRING}" "^id=/(.*)" [NC]
RewriteRule "(.*)" "$1%1?" [R=301,L]
OR within your virtualhost.conf
-file:
Here the context is essential:
<VirtualHost *:80>
DocumentRoot "/var/www/vhosts/myhost/htdoc/"
# Other directives
# Only required once:
RewriteEngine On
<Directory "/var/www/vhosts/myhost/htdoc/news-and-events/news/">
RewriteBase "/news-and-events/news/"
RewriteCond "%{QUERY_STRING}" "^id=/(.*)" [NC]
RewriteRule "(.*)" "$1%1?" [R=301,L]
</Directory>
# More <Directory> container
</VirtualHost>
BTW: It is a good habit to enclose the patterns in double quotes, even if not always necessary.
Some Comments
You match against QUERY_STRING
:
QUERY_STRING = 'id=/nodle/nodle-io/ledger-integration-update-compatibility-status-236699676dc6'
It will always begin with the character after the ?
in the URL.
So the match in the RewriteCond
does not
As the regex is greedy and you select the whole string forcing the ^
start and $
stop of the string is obsolete. Also making it optional by ?
is unnecessary, as (.*)
may evaluate to empty by itself.
Notes
- Meanwhile you've disclosed, that the code shall reside in a
virtualhost.conf
-file of the apache server. - Also the configuration is not standalone but a sub-directory of a WordPress installation, that also uses some Rewrite rules.
As you have enabled the directive for.htaccess
, but want to write the rules in thevirtualhost.conf
-file. - Using the propper <Directory> container is essential to prohibit cross-influences with your WordPress installation (in both directions).
AllowOverride All
you should first cleanup all possibly still existing previous "experiments" within .htaccess
files to solve your problem.
— Really important!
But obviously your intent has been the inverse of that what has been your question:
- Apache hide query string in URL but keep URL working
- Php to remove query string in URL but keep URL working
So besides the fact, the presented solution does what you've asked, there is obviously no content at the rewritten URL and you end up with a 404 · Not found
. HTML client error.
Doing something like this has its sense, when it comes to a changed URI-structure (i.e. because of a change of the serving software) and you want your visitors to be able to follow the content. Also by the HTML status code 301 · Moved permanently
the search engines will quickly learn to use the new URI logic and keep the SEO up.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论