php - .htaccess 内部重定向不适用于字符串参数

标签 php wordpress .htaccess redirect mod-rewrite

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

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

此重定向在 cid 为数字时有效,但在使用像 "my-key" 这样的字符串时无效。使用 key ,我必须将规则更改为此。

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

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

编辑完成.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

最佳答案

我在本地做了一些尝试,发现如果我设置以下两个规则,它对我有用:

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 fo)。在我的主机上,我启用了 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 时会进行其他一些日志记录)

关于php - .htaccess 内部重定向不适用于字符串参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45341889/

相关文章:

.htaccess - htaccess 与子目录中的 Symfony,无法隐藏/web

javascript - 使用 json_decode 在 php 中获取 json 数组的问题

javascript - Google 登录扩展 token

php - 使 Wordpress wp_delete_attachment 从预定义的自定义文件夹中删除文件

php - WordPress 子菜单不起作用

apache - 带标志的重定向 301

php哈希形式字符串到整数

PHP 数据处理失败,错误不明确

php - 如何在登录按钮下方添加 HTML?

php - 在没有扩展的情况下运行 PHP 页面