Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.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/2/ionic-framework/2.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
Regex 需要重写url中的问号和等号_Regex_.htaccess_Mod Rewrite_Url Rewriting_Clean Urls - Fatal编程技术网

Regex 需要重写url中的问号和等号

Regex 需要重写url中的问号和等号,regex,.htaccess,mod-rewrite,url-rewriting,clean-urls,Regex,.htaccess,Mod Rewrite,Url Rewriting,Clean Urls,重写了我的网址。但是,我仍然可以访问带有问号和加号的重写URL lovelakedistrict.com/lake-district-cottages/?cottages=2/ lovelakedistrict.com/lake-district-cottages/?cottages/2/ lovelakedistrict.com/lake-district-cottages/cottages/2/ 上面的三个url是完全相同的页面,我想正确地重新编写它们,以便它们重定向到正确的结构(最后一个

重写了我的网址。但是,我仍然可以访问带有问号和加号的重写URL

lovelakedistrict.com/lake-district-cottages/?cottages=2/
lovelakedistrict.com/lake-district-cottages/?cottages/2/
lovelakedistrict.com/lake-district-cottages/cottages/2/
上面的三个url是完全相同的页面,我想正确地重新编写它们,以便它们重定向到正确的结构(最后一个url),以防止网页重复

Options +FollowSymlinks
Options +Indexes
RewriteEngine on

RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^.#?\ ]+)\.php([#?][^\ ]*)?\ HTTP/

RewriteCond %1 !^include/
RewriteRule ^([^.]+)\.php$ /$1 [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ $1.php

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]

RewriteRule ^lake-district-cottages/cottages/([0-9]+) lake-district-cottages.php?cottages=$1
试试这些规则:

RewriteCond %{THE_REQUEST} ^[A-Z]+\ /[^?\ ]*\?[^\ ]+
RewriteCond %{QUERY_STRING} ^/*([^=&]*[^=&/])/*(&+(.*))$
RewriteRule ^(.*[^/])/?$ /$1/%1/?%3 [N]

RewriteCond %{THE_REQUEST} ^[A-Z]+\ /[^?\ ]*\?[^\ ]+
RewriteCond %{QUERY_STRING} ^/*([^=&]*[^=&/])/*$
RewriteRule ^(.*[^/])/?$ /$1/%1/?%4 [L,R=301]

RewriteCond %{THE_REQUEST} ^[A-Z]+\ /[^?\ ]*\?[^\ ]+
RewriteCond %{QUERY_STRING} ^/*([^=&]*[^=&/])/*=/*([^&]*[^&/])/*(&+(.*))$
RewriteRule ^(.*[^/])/?$ /$1/%1/%2/?%4 [N]

RewriteCond %{THE_REQUEST} ^[A-Z]+\ /[^?\ ]*\?[^\ ]+
RewriteCond %{QUERY_STRING} ^/*([^=&]*[^=&/])/*=/*([^&]*[^&/])/*$
RewriteRule ^(.*[^/])/?$ /$1/%1/%2/? [L,R=301]
但我想最简单的方法是使用比PHP等mod_重写功能更强大的语言:

$parts = explode('?', $_SERVER['REQUEST_URI'], 2);
if (count($parts) === 2) {
    $path = rtrim($parts[0], '/');
    parse_str($parts[1], $params);
    foreach ($params as $key => $value) {
        $path .= '/' . (($value === '') ? trim($key, '/') : trim($key, '/') . '/' . trim($value, '/'));
    }
    header('Location: http://example.com'.$path.'/', true, 301);
    exit;
}

Htaccess规则工作得很好,除了当我尝试这个例子时,它直接指向(在php扩展上添加)-这可以删除吗?谢谢你的快速回复reply@ajfmedia:这可能是由于您使用这些规则的顺序。确保将导致外部重定向的规则放在导致内部重定向的规则之前。还有一件事,Gumbo,有没有办法阻止影响结果目录中anthing的规则?例如,因为它会弄乱我的搜索查询字符串:(-虽然它确实整理了它们!@ajfmedia:是的,您可以向每个规则添加以下条件:
RewriteCond%{REQUEST_URI}!^/result/
。或者将此规则放在其他规则之前跳过它们:
RewriteRule^result/-[S=4]
。嘿,Gumbo,很抱歉打扰你,你能帮我看些别的吗?-我不想发布新问题,因为我觉得它们都差不多。