简体   繁体   English

htaccess 301使用动态页面重定向?

[英]htaccess 301 redirect with a dynamic page?

In htaccess, how do you 301 redirect dynamic pages? 在htaccess中,您如何301重定向动态页面?

For example, what if I wanted a rule that made /get.php?i=1234 to redirect to /i/1234 , etcetera? 例如,如果我想要使/get.php?i=1234重定向到/i/1234等的规则怎么办?

You can use mod_rewrite like redirection as in this code: 您可以像下面的代码一样使用mod_rewrite进行重定向:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteCond %{QUERY_STRING} ^(i)=([^&]+)(&|$) [NC]
RewriteRule ^get\.php$ %1/%2? [L,R=301,NC]

Try the RedirectMatch directive with a regular expression . 使用正则表达式尝试RedirectMatch指令 It depends on mod_alias so that's probably always enabled. 它取决于mod_alias,因此可能始终处于启用状态。 You can use mod_rewrite for that too but it might be overkill. 您也可以使用mod_rewrite,但这可能会过大。 The replacement may contain references in the form of $n: 替换内容可能包含$ n形式的引用:

RedirectMatch 301 ^/get\.php\?i=([0-9]+)$ /i/$1

As you can imagine, $1 is a reference to what's matched inside the pair of parenthesis #1 . 可以想象, $1对括号#1中匹配的内容的引用 You can have many references: 您可以有许多参考资料:

RedirectMatch 301 ^/get\.php\?i=([0-9]+)&y=([0-9]+)$ /i/$1/$2/

You can use an online regular expression tester to elaborate your apache config: if i put ^/get\\.php\\?i=([0-9]+)&y=([0-9]+)$ in the "Regexp" input, and /get.php?i=12&y=24 in the "Subject string" input, then click "Show match" it shows: 您可以使用在线正则表达式测试仪来详细说明您的apache配置:如果我在“ Regexp”中放入^/get\\.php\\?i=([0-9]+)&y=([0-9]+)$输入,然后在“主题字符串”输入中/get.php?i=12&y=24 ,然后单击“显示匹配项”,它显示:

Match at position 0:
/get.php?i=12&y=24
12  # first reference available
24  # second reference available

If your regexps don't work in the tester, they not likely to work in an apache configuration. 如果您的正则表达式在测试仪中不起作用,则它们不太可能在apache配置中工作。 You should try them in the tester first. 您应该首先在测试仪中尝试它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM