【问题标题】:Redirect according to the URL根据 URL 重定向
【发布时间】:2016-03-10 21:35:57
【问题描述】:

我有以下情况: 我想做一个这样的重定向系统:

/redirect/place1   ->  /page.php?place=place1
/redirect/testabc  ->  /page.php?place=testabc
/redirect/xyzw123  ->  /page.php?place=xyzw123

我现在能看到的唯一方法是在 /redirect 中创建具有重定向它的 index.php 的子文件夹。但我希望它是全自动的。

感谢您的任何回答!

【问题讨论】:

  • Apache 服务器? .htaccess 文件中的 301 重定向是可行的方法。

标签: html apache .htaccess redirect web


【解决方案1】:

如果你想将 /redirect/(Anything) 重定向到 /page.php?place=(Same as Anything)

您可以使用来自mod_alias 的重定向命令:

Redirect 301 /redirect/ /page.php?place=

你想使用 mod_rewrite 的 RewriteRule 命令

RewriteRule /redirect/(.*) /page.php?place=$1 [L]

更多信息:

  • [L]是一种重定向,如果你想做一个重定向条件可以加一个状态码,比如[L,R=301]会重定向发送301重定向永久

    李>
  • 在这里您可以找到所有用于重定向的 http 代码 https://fr.wikipedia.org/wiki/Liste_des_codes_HTTP

  • 在这里你可以找到重写条件的所有标志 https://httpd.apache.org/docs/2.4/rewrite/flags.html

  • 如果您只想在页面或文件夹不存在时重定向:

    RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
    RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-d
    RewriteRule /redirect/(.*) /page.php?place=$1 [L]
    

!-d 表示找不到文件夹

!-f 表示找不到文件

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-10
    • 1970-01-01
    • 2017-09-20
    • 2020-01-23
    相关资源
    最近更新 更多