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

发布于 2024-09-17 05:26:11 字数 923 浏览 4 评论 0原文

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

lovelakedistrict.com/lake-district-cottages/?cottages=2/
lovelakedistrict.com/lake-district-cottages/?cottages/2/
lovelakedistrict.com/lake-district-cottages/cottages/2/

上面的三个网址是完全相同的页面,我想正确地重写它们,以便它们重定向到正确的结构(最后一个网址)以停止网页重复。

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

Rewritten my url. However I can still access rewritten urls with question marks and plus signs.

lovelakedistrict.com/lake-district-cottages/?cottages=2/
lovelakedistrict.com/lake-district-cottages/?cottages/2/
lovelakedistrict.com/lake-district-cottages/cottages/2/

The three urls above are the exact same page, I would like to properly re-write them so they redirect to the correct structure (the last url) to stop duplication of webpages.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

翻身的咸鱼 2024-09-24 05:26:20

尝试这些规则:

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]

但我想最简单的方法是使用比 mod_rewrite 更强大的语言,例如 PHP:

$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;
}

Try these rules:

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]

But I guess the easiest would be to use a more powerful language than mod_rewrite like PHP:

$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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文