.htaccess 中用于 index.php 查询参数的 URL RewriteRule [英] URL RewriteRule in .htaccess for index.php query parameters

查看:24
本文介绍了.htaccess 中用于 index.php 查询参数的 URL RewriteRule的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

示例网址:-

  • www.domain.com/index.php?bob
  • www.domain.com/index.php?jane
  • www.domain.com/index.php?fred

需要重写如下:-

  • www.domain.com/bob
  • www.domain.com/jane
  • www.domain.com/fred

现在已经尝试了很多变体,但我能得到的最接近的是:-

Have tried with many variations now but the closest I can get to is:-

  • www.domain.com/?bob
  • www.domain.com/?jane
  • www.domain.com/?fred

.htaccess 中的下面实现了这个...

The below in .htaccess achieves this...

RewriteRule ^(.*)$ index.php?$1 [L,QSA]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
RewriteRule ^ %1 [R=301,L]

请有人指出我需要修改什么才能将 .网址中的(问号)?

Please could someone point out what I need to modify to bin the ? (question mark) in the URL?

刚刚注意到,由于应用了 anubhava 给出的答案,例如在 robots.txt 下方并不能解析为.txt 文件,但只显示主页.

Just noticed that since applying the answer given by anubhava below that robots.txt for example doesn't resolve to the .txt file but just displays the homepage.

.htaccess 下面:-

.htaccess below:-

RewriteEngine On

RewriteBase /
RewriteCond %{THE_REQUEST} \s/+index\.php\?([^\s&]+) [NC]
RewriteRule ^ %1? [R=301,L]

RewriteRule ^sitemap.xml$ index.php?route=feed/google_sitemap [L]
RewriteRule ^googlebase.xml$ index.php?route=feed/google_base [L]
RewriteRule ^download/(.*) /index.php?route=error/not_found [L]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

#pos1
RewriteRule ^([^/]+)/?$ index.php?$1 [L,QSA] <--

RewriteCond %{REQUEST_URI} !.*\.(ico|gif|jpg|jpeg|png|js|css)
RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]

#pos2

如果我在 pos1 中添加 <-- 行,robots.txt URL 将返回 404 page not found 错误.

If I add the <-- line in pos1, the robots.txt URL returns a 404 page not found error.

如果我在 pos2 中添加 <-- 行,robots.txt URL 只会显示主页.

If I add the <-- line in pos2, the robots.txt URL just displays the homepage.

与此同时,我通过在 Rewrite Base/ 下添加以下内容,将 robots.txt 从重写中排除:-

In the meantime, I have excluded robots.txt from rewrites by adding the following under Rewrite Base /:-

RewriteRule ^robots.txt - [L]

推荐答案

您可以使用以下代码:

RewriteEngine On

RewriteCond %{THE_REQUEST} \s/+index\.php\?([^\s&]+) [NC]
RewriteRule ^ %1? [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ index.php?$1 [L,QSA]

这篇关于.htaccess 中用于 index.php 查询参数的 URL RewriteRule的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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