.htaccess内部重定向不使用字符串参数

时间:2017-07-27 05:12:24

标签: php wordpress .htaccess redirect mod-rewrite

所以我已经在这几个小时了,但无法解决这个问题。 stackoverflow上没有任何内容可以解决我的问题。问题是我的.htaccess中有一个重定向规则如下

RewriteRule ^store-category/(.+)/?$ /wordpress/store-category/?cid=$1 [QSA,L]

当cid是一个数字时,此重定向有效但当使用像" my-key" 这样的字符串时,它不起作用。使用密钥我必须将规则更改为此。

RewriteRule ^store-category/(.+)/?$ /wordpress/store-category/?cid=$1 [R,L]

但是我不想使用它,因为这会明显地重定向我的用户并且网址会发生变化,这是我不想要的。任何人都可以解释为什么这个东西适用于数字但不适用于字符串参数。任何帮助将不胜感激。

修改:完成.htaccess

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /wordpress/
RewriteRule ^index\.php$ - [L]
RewriteRule ^store-category/(.+)/?$ /wordpress/store-category/?cid=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wordpress/index.php [L]
</IfModule>

# END WordPress

2 个答案:

答案 0 :(得分:1)

我在本地做了一些尝试,发现如果我把这两条规则适用于我:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

甚至在行

之前
RewriteRule ^store-category/(.+)/?$ /wordpress/store-category/?cid=$1 [QSA,L]

完整的.htaccess文件将是:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /wordpress/

RewriteRule ^index\.php$ - [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^store-category/(.+)/?$ /wordpress/store-category/?cid=$1 [QSA,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wordpress/index.php [L]
</IfModule>

无论如何,使用(.+)/?$,将从组中捕获最终的尾部斜杠,并将继续cid个参数。如果类别内部没有斜杠,则最好使用([^/]+)/?$

更新:了解重定向发生的情况,启用日志(请参阅此http://httpd.apache.org/docs/current/mod/mod_rewrite.html)。在我的主机上,我启用了mod_rewrite.c:trace3,我在error_log中获得了以下日志(仅跟踪rewrite

[Fri Aug 04 12:48:56.904099 2017] [rewrite:trace3] [pid 22823:tid 140368028878592] mod_rewrite.c(477): [client ::1:58312] ::1 - - [localhost/sid#788520][rid#7fa9e4002970/initial] [perdir /var/www/localhost/htdocs/wordpress/] strip per-dir prefix: /var/www/localhost/htdocs/wordpress/store-category/my-id -> store-category/my-id
[Fri Aug 04 12:48:56.904123 2017] [rewrite:trace3] [pid 22823:tid 140368028878592] mod_rewrite.c(477): [client ::1:58312] ::1 - - [localhost/sid#788520][rid#7fa9e4002970/initial] [perdir /var/www/localhost/htdocs/wordpress/] applying pattern '^index\\.php$' to uri 'store-category/my-id'
[Fri Aug 04 12:48:56.904129 2017] [rewrite:trace3] [pid 22823:tid 140368028878592] mod_rewrite.c(477): [client ::1:58312] ::1 - - [localhost/sid#788520][rid#7fa9e4002970/initial] [perdir /var/www/localhost/htdocs/wordpress/] strip per-dir prefix: /var/www/localhost/htdocs/wordpress/store-category/my-id -> store-category/my-id
[Fri Aug 04 12:48:56.904132 2017] [rewrite:trace3] [pid 22823:tid 140368028878592] mod_rewrite.c(477): [client ::1:58312] ::1 - - [localhost/sid#788520][rid#7fa9e4002970/initial] [perdir /var/www/localhost/htdocs/wordpress/] applying pattern '^store-category/(.+)/?$' to uri 'store-category/my-id'
[Fri Aug 04 12:48:56.904143 2017] [rewrite:trace2] [pid 22823:tid 140368028878592] mod_rewrite.c(477): [client ::1:58312] ::1 - - [localhost/sid#788520][rid#7fa9e4002970/initial] [perdir /var/www/localhost/htdocs/wordpress/] rewrite 'store-category/my-id' -> '/wordpress/store-category/?cid=my-id'
[Fri Aug 04 12:48:56.904147 2017] [rewrite:trace3] [pid 22823:tid 140368028878592] mod_rewrite.c(477): [client ::1:58312] ::1 - - [localhost/sid#788520][rid#7fa9e4002970/initial] split uri=/wordpress/store-category/?cid=my-id -> uri=/wordpress/store-category/, args=cid=my-id
[Fri Aug 04 12:48:56.904151 2017] [rewrite:trace2] [pid 22823:tid 140368028878592] mod_rewrite.c(477): [client ::1:58312] ::1 - - [localhost/sid#788520][rid#7fa9e4002970/initial] [perdir /var/www/localhost/htdocs/wordpress/] trying to replace prefix /var/www/localhost/htdocs/wordpress/ with /wordpress/
[Fri Aug 04 12:48:56.904155 2017] [rewrite:trace2] [pid 22823:tid 140368028878592] mod_rewrite.c(477): [client ::1:58312] ::1 - - [localhost/sid#788520][rid#7fa9e4002970/initial] [perdir /var/www/localhost/htdocs/wordpress/] trying to replace context docroot /var/www/localhost/htdocs with context prefix 
[Fri Aug 04 12:48:56.904158 2017] [rewrite:trace1] [pid 22823:tid 140368028878592] mod_rewrite.c(477): [client ::1:58312] ::1 - - [localhost/sid#788520][rid#7fa9e4002970/initial] [perdir /var/www/localhost/htdocs/wordpress/] internal redirect with /wordpress/store-category/ [INTERNAL REDIRECT]

(当其尝试查找默认文档index.php时)进行其他一些日志记录

答案 1 :(得分:0)

正则表达式贪婪(从左到右)。你可能会在其他方面拉出额外的斜线。我觉得最简单的方法就是不使用URL类(很少&#39;。&#39;它会匹配所有内容)。

RewriteRule ^store-category/([^\/]+)/?$ store-category/?cid=$1 [QSA,L]

您也可以简化为此,具体取决于您希望处理商店类别ID后可能格式错误的网址。

RewriteRule ^store-category/([^\/]+) store-category/?cid=$1 [QSA,L]
编辑:你是对的。我没有测试整个htaccess。您在rewritebase中再次指定wordpress路径两次,并在规则中再次指定。我已经更新了上述规则,并在测试人员中提供了更新解决方案的链接:

http://htaccess.mwl.be?share=116de701-cf8a-577c-bab5-5c357d844452