apache - 将带有参数的 url 重定向到可读的 url

标签 apache .htaccess mod-rewrite redirect seo

我最近使用以下 htaccess 将带有参数的 url 更改为更具可读性的 url:

RewriteRule ^page/([A-Za-z0-9-+_\s]+)$ /index.php?n=$1 [L]
RewriteRule ^category/([A-Za-z0-9-+_\s]+)/([0-9]+)$ /index.php?p=$1&b=$2 [L]

我相信这会导致搜索引擎上出现重复内容的问题,因为 index.php url 和 domain.com/page/url 都将被编入索引。

如果对 domain.com/page/1 的调用被重定向到 index.php?n=1 我该如何重定向对 index.php 的请求? phpdomain.com/page/ ??

我这样做的尝试是给我一个 404 代码,将我发送到 domain2。

#RewriteCond %{QUERY_STRING} ^n=([A-Za-z0-9]+)$ [NC]
#RewriteRule ^index\.php$ http://domain.com/page/? [R=301,L]


htaccess

IndexIgnore *
ErrorDocument 404 http://domain2.com


#REDIRECTS
Redirect 301 /keyword.php http://domain.com
Redirect 301 /inc/keyword.php http://domain.com




Options +FollowSymlinks
RewriteEngine on

#Redirect all pages in a folder to a single URL
RewriteRule ^test/(.*)$ http://domain.com [L,R=301]

# HTTP HOST, escape . and start string with ^
RewriteCond %{HTTP_HOST} ^www\.domain\.com [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [R=301,NC] 


RewriteCond %{QUERY_STRING} ^p=h&m=([A-Za-z]+)&y=([0-9]+)$ [NC]
RewriteRule ^index\.php$ http://domain.com? [R=301,L]


#RewriteCond %{QUERY_STRING} ^n=([A-Za-z0-9]+)$ [NC]
#RewriteRule ^index\.php$ http://domain.com/page/? [R=301,L]


RewriteRule ^page/([A-Za-z0-9-+_\s]+)$ /index.php?n=$1 [L]

RewriteRule ^category/([A-Za-z0-9-+_\s]+)/([0-9]+)$ /index.php?p=$1&b=$2 [L]

最佳答案

代替您评论的 301 规则试试这个:

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

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

关于apache - 将带有参数的 url 重定向到可读的 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23389126/

相关文章:

PHP 代码呈现为文本

ruby-on-rails - Passenger + Capistrano + Apache = Rails 应用程序未运行

php - 如何在子文件夹中设置索引页

apache - 使用参数重定向 URL

Django Rest 框架反向关系在代理后面时中断

apache - 出站连接( curl ,套接字)不适用于 apache 但以 root 身份工作

.htaccess - Laravel 访问

PHP:处理数千个条目,脚本在一定数量后终止

mysql - 我需要更改数百篇 wordpress 文章中的 url。帮助?

apache - htaccess mod_rewrite 未在实时服务器上传递获取变量