apache - 我可以使用 HTACCESS 来格式化此 URL 吗?

标签 apache .htaccess redirect mod-rewrite url-rewriting

我想知道是否可以将 https://site1.com/en/article-details.test-article?item=test 之类的 URL 格式化为 https ://site1.com/en/article-details/test-article?item=test 所以基本上将 .test-article 更改为 /test-article >.

由于我的网站的工作方式,它仅将 .test-article 识别为有效选择器,因此请阅读 https://httpd.apache.org/docs/2.4/rewrite/flags.html看来我可能需要使用 PT 标志向用户显示所需的 URL,但将正确的 URL 传递到我的网站。

到目前为止,我用这个来替换斜线的点

RewriteCond %{REQUEST_URI} ^/([a-z]{2})/article-details.([a-z]{2})(/?)$
RewriteRule ^/([a-z]{2})/.*  https://%{SERVER_NAME}/$1/$2 [L,NC,R=301]

编辑(这可以根据 RavinderSingh13 的建议格式化 URL)

RewriteEngine ON
RewriteCond %{REQUEST_URI} ^/([a-z]{2})/article-details.([^?]*)(/?)$
RewriteRule ^ https://%{SERVER_NAME}/%1/article-details/%2 [QSA,R=301,NE,L]

最佳答案

根据您显示的示例,您可以尝试以下操作吗?请确保在测试您的网址之前清除浏览器缓存。

RewriteEngine ON
RewriteCond %{THE_REQUEST} \s/(en/article-details)\.(test-article)\?item=test\s [NC]
RewriteRule ^ https://site1.com/%1/%2 [QSA,R=301,NE,L]

根据OP的评论,在此处添加更多通用规则:

RewriteEngine ON
RewriteCond %{THE_REQUEST} \s/(en/article-details)\.([^?]*)\?item=test\s [NC]
RewriteRule ^ https://site1.com/%1/%2 [QSA,R=301,NE,L]

关于apache - 我可以使用 HTACCESS 来格式化此 URL 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66158915/

相关文章:

apache - 如何保持 apache 作为前端和 tomcat 作为后端?

php - 将所有请求重定向到非 www ssl https

jsp - 如何处理先前由servlet 转发的页面上的请求?

php - 如何在 .htaccess 中为 'pagename/id' 设置重写规则?

php - 使用 php 为 apache 日志设置 REMOTE_USER

php - Centos 7 Apache 和 PHP5 错误 : Package: php-5. 6.40-21.el6.remi.x86_64 (remi-php56)

ruby-on-rails - 将我的 Wordpress 站点移动到子域

html - 如何从 URL 中删除 .html?

.htaccess - Angular 2在刷新时找不到路线

reactjs - 为什么 Github 页面 url 在加载时发生变化,导致公共(public)资源路径不正确?