如何使用参数重定向 url?

发布于 2024-11-15 15:14:13 字数 1499 浏览 2 评论 0原文

我在尝试将 mod_rewrite 集成到现有页面中时完全不知所措。目前,我正在从头开始构建一个新站点,并且有一些漂亮的干净 url,例如:

http://www.example.nl/nl/example

运行 Cms 的旧站点变得简单,有一些未重写的 url,需要重定向到新页面。这些网址看起来像这样:

http://www.example.nl/index.php?page=cake-and-pie&hl=nl_NL

但更短的版本如:

http://www.example.nl/index.php?page=cake-and-pie

也可以。

我花了一段时间才发现带有参数的 url 不能简单地使用“重定向 301”进行重定向,就像我通常所做的那样。所以我尝试了一些在线 mod_rewrite 生成器,例如 thisthis,但这些输出的规则只会导致 404 错误(重定向根本不起作用)。

我的 .htaccess 文件当前如下所示:

RewriteEngine On
RewriteBase /

# remove .php;
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
RewriteRule (.*)\.php$ $1 [R=301]

# remove index
RewriteRule (.*)/index$ $1/ [R=301]

# remove slash if not directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/ $1 [R=301]

# add .php to access file, but don't redirect
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]

旧页面将抓住存在。 如何将旧页面重定向到新页面?

谢谢。

编辑

RewriteCond %{QUERY_STRING} =page=pie
RewriteRule ^index\.php$ /nl/? [L,R=301]
RewriteCond %{QUERY_STRING} =page=pie&hl=nl_NL
RewriteRule ^index\.php$ /nl/? [L,R=301]

似乎可以解决问题。这当然是每个网址的手册,但我只有几个。

I'm at a total loss trying to integrate a mod_rewrite in my existing page. Currently, I am building a new site from scratch, and there i have some nice clean url's such as:

http://www.example.nl/nl/example

The old site, running Cms made simple, has some not-rewritten url's that would need to be redirected to the new pages. Those url's look like this:

http://www.example.nl/index.php?page=cake-and-pie&hl=nl_NL

But shorter versions of that like:

http://www.example.nl/index.php?page=cake-and-pie

also work.

It took me a while to figure out that url's with parameters cannot simply be redirected with "Redirect 301", like i'd normaly do. So i tried some online mod_rewrite generators like this and this, but the rules outputted by those result only in 404 errors, (the redirect doesn't work at all).

My .htaccess file current looks like this:

RewriteEngine On
RewriteBase /

# remove .php;
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
RewriteRule (.*)\.php$ $1 [R=301]

# remove index
RewriteRule (.*)/index$ $1/ [R=301]

# remove slash if not directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/ $1 [R=301]

# add .php to access file, but don't redirect
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]

The old pages would seize to exist.
How do i redirect the old pages to the new ones?

Thanks.

EDIT

RewriteCond %{QUERY_STRING} =page=pie
RewriteRule ^index\.php$ /nl/? [L,R=301]
RewriteCond %{QUERY_STRING} =page=pie&hl=nl_NL
RewriteRule ^index\.php$ /nl/? [L,R=301]

Seems to do the trick. This is of course manual for every url, but i only have a few.

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

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

发布评论

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

评论(1

七分※倦醒 2024-11-22 15:14:13
RewriteEngine On
RewriteBase /
RewriteRule ^(.+)/([^/]+)/?$    index.php?page=$1&hl=$2
RewriteRule ^([^/]+)/?$         index.php?page=$1 [QSA]
RewriteEngine On
RewriteBase /
RewriteRule ^(.+)/([^/]+)/?$    index.php?page=$1&hl=$2
RewriteRule ^([^/]+)/?$         index.php?page=$1 [QSA]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文