Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Apache mod_重写和已编制索引的页面_Apache_.htaccess_Mod Rewrite_Search Engine - Fatal编程技术网

Apache mod_重写和已编制索引的页面

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页面。 当

我有一个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
页面。 当他们要求
/profile.php?id=123
时,我如何引导他们或任何其他用户切换到
/users/123

为了确保这一点,我需要这样做:

  • 用户请求
    /profile.php?id=123
  • 用户被重定向到带有301状态代码的
    /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]