.htaccess 重写重定向
我有一些在 .htaccess 文件中使用重写引擎的代码,它指定了我的 SERP 的目录。但是,当有人进入 search/QUERY/ 而不是 search/QUERY/PAGE/ 时,它会显示 404 错误。与搜索/相同。
我希望这样,如果有人只是去搜索/QUERY/,它会将他们重定向到搜索/QUERY/1/,而对于搜索/,它会将他们重定向到我的主页/。我在下面包含了我的代码的副本。
RewriteEngine on
RewriteRule ^search/([^/]+)/([^/]+)/?$ search.php?q=$1&category=web&d=$2
谁能帮我解决这个问题吗?
预先感谢,卡勒姆
I have some code which uses Rewrite engine in my .htaccess file and it specifies the directory for my SERPs. However when someone goes onto search/QUERY/ rather than search/QUERY/PAGE/ it displays a 404 error. Same with just search/.
I want it so that if someone just goes to search/QUERY/ that it redirects them to search/QUERY/1/ and for just search/ it redirects them to my homepage /. I have included a copy of my code below.
RewriteEngine on
RewriteRule ^search/([^/]+)/([^/]+)/?$ search.php?q=$1&category=web&d=$2
Can anyone help me with this problem?
Thanks in advance, Callum
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在您的 htaccess 中尝试以下代码:
Try this code in your htaccess :
您可以创建多个
RewriteRule
语句来完成此操作。 (如果使用中间规则,请确保省略中间规则上的[R]
标志!)您发布的规则仅在格式为
/search/query/n/,因此编写一个匹配
search/query
的正则表达式,重定向到/search/query/1
。执行相同的操作以重定向主页,无需查询。You can create multiple
RewriteRule
statements to accomplish this. (Make sure to omit the[R]
flag on intermediate rules if you use it!)The rule you posted will only rewrite if it's of the form
/search/query/n/
, so write a regular expression matchingsearch/query
that redirects to/search/query/1
. Do the same to redirect home with no query.在 .htaccess 文件中尝试以下另外 2 条规则:
Try these additional 2 rules in your .htaccess file: