Apache mod_重写和已编制索引的页面
我有一个PHP页面,比如说Apache mod_重写和已编制索引的页面,apache,.htaccess,mod-rewrite,search-engine,Apache,.htaccess,Mod Rewrite,Search Engine,我有一个PHP页面,比如说/users/profile.PHP,我需要使URL更加优雅,比如/users/123,而不是/users/profile.PHP?id=123。 我定义了所需的规则,即在/users/123上执行/users/profile.php?id=123,如下所示: RewriteRule ^users/([0-9]+)$ /users/profile.php?id=$1 [L] 一切都很完美,但搜索引擎已经熟悉了/users/profile.php?id=123页面。 当
/users/profile.PHP
,我需要使URL更加优雅,比如/users/123
,而不是/users/profile.PHP?id=123
。
我定义了所需的规则,即在/users/123
上执行/users/profile.php?id=123
,如下所示:
RewriteRule ^users/([0-9]+)$ /users/profile.php?id=$1 [L]
一切都很完美,但搜索引擎已经熟悉了/users/profile.php?id=123
页面。
当他们要求/profile.php?id=123
时,我如何引导他们或任何其他用户切换到/users/123
为了确保这一点,我需要这样做:
/profile.php?id=123
/users/123
/profile.php?id=123
并将HTML返回给用户/users/profile.php?id=123
重定向到/users/123
:
RewriteCond %{QUERY_STRING} ^id=([0-9]+)$
RewriteRule ^/users/profile\.php$ /user/%1 [R=307,L]
内部转发/user/123
到/users/profile.php
:
RewriteRule ^user/([0-9]+)$ /users/profile.php?id=$1 [L]
我将/users
更改为/user
,因为URL描述的是单个用户
(这些都来自内存,没有实际测试。但基本思想是第一条规则中的
[R=307]
标志。)使用.htaccess
:
RewriteRule ^users/(\d+)/?$ /users/profile.php?id=$1 [L]
RewriteCond %{QUERY_STRING} ^id=(\d+)$
RewriteRule ^users/profile\.php$ /users/%1 [R=301,L]
为了避免循环,我会更改通常没有人需要知道的页面名称(profile.php->profileNEW.php)
,在HTMLY中,您可能希望使用301(永久移动)而不是307(临时重定向)。有这样的循环。我检查了更多,唯一的方法是重命名(隐藏..)PHP页面。这不是唯一的方法,但在我看来,这是最简单的方法。
RewriteRule ^users/(\d+)/?$ /users/profile.php?id=$1 [L]
RewriteCond %{QUERY_STRING} ^id=(\d+)$
RewriteRule ^users/profile\.php$ /users/%1 [R=301,L]