【问题标题】:On a LAMP server, I want the URL http://example.com/index.php to be rewritten to http://example.com. How do I do that?在 LAMP 服务器上,我希望将 URL http://example.com/index.php 重写为 http://example.com。我怎么做?
【发布时间】:2011-04-15 18:09:24
【问题描述】:

在 LAMP 服务器上,我想要 URL

http://example.com/index.php
被重写为简单的
http://example.com

我目前的.htaccess文件如下...

IndexIgnore *

ErrorDocument 400 /index.php?module=error&action=error
ErrorDocument 401 /index.php?module=error&action=error
ErrorDocument 403 /index.php?module=error&action=error
ErrorDocument 404 /index.php?module=error&action=error
ErrorDocument 500 /index.php?module=error&action=error

RedirectMatch 301 ^/media/$ /
RedirectMatch 301 ^/media/documents/$ /
RedirectMatch 301 ^/media/graphics/$ /
RedirectMatch 301 ^/media/photos/$ /
RedirectMatch 301 ^/library/$ /
RedirectMatch 301 ^/library/css/$ /
RedirectMatch 301 ^/library/ht/$ /
RedirectMatch 301 ^/library/js/$ /
RedirectMatch 301 ^/library/php/$ /

RewriteEngine on
RewriteBase /

RewriteRule ^home$ /index.php?module=home&action=frontpage
RewriteRule ^home/$ /index.php?module=home&action=frontpage
RewriteRule ^home/([^/\.]+)$ /index.php?module=home&action=$1
RewriteRule ^home/([^/\.]+)/$ /index.php?module=home&action=$1

RewriteRule ^cv$ /index.php?module=home&action=cv
RewriteRule ^cv/$ /index.php?module=home&action=cv

RewriteRule ^release$ /index.php?module=release&action=release
RewriteRule ^release/$ /index.php?module=release&action=release

RewriteRule ^photos$ /index.php?module=gallery&action=album&album=general
RewriteRule ^photos/$ /index.php?module=gallery&action=album&album=general

RewriteRule ^gallery$ /index.php?module=gallery&action=album&album=general
RewriteRule ^gallery/$ /index.php?module=gallery&action=album&album=general
RewriteRule ^gallery/([^/\.]+)$ /index.php?module=gallery&action=album&album=$1
RewriteRule ^gallery/([^/\.]+)/$ /index.php?module=gallery&action=album&album=$1
RewriteRule ^gallery/([^/\.]+)/([^/\.]+)$ /index.php?module=gallery&action=album&album=$1$&page=$2
RewriteRule ^gallery/([^/\.]+)/([^/\.]+)/$ /index.php?module=gallery&action=album&album=$1$&page=$2
RewriteRule ^gallery/([^/\.]+)/([^/\.]+)/([^/\.]+)$ /index.php?module=gallery&action=item&album=$1$&page=$2&item=$3
RewriteRule ^gallery/([^/\.]+)/([^/\.]+)/([^/\.]+)/$ /index.php?module=gallery&action=item&album=$1$&page=$2&page=$3

RewriteRule ^handouts$ /index.php?module=home&action=handouts
RewriteRule ^handouts/$ /index.php?module=home&action=handouts

RewriteRule ^links$ /index.php?module=home&action=links
RewriteRule ^links/$ /index.php?module=home&action=links

RewriteRule ^contact$ /index.php?module=home&action=contact
RewriteRule ^contact/$ /index.php?module=home&action=contact

RewriteRule ^login$ /index.php?module=authentication&action=login
RewriteRule ^login/$ /index.php?module=authentication&action=login
RewriteRule ^logout$ /index.php?module=authentication&action=logout
RewriteRule ^logout/$ /index.php?module=authentication&action=logout

RewriteRule ^copyright$ /index.php?module=home&action=copyright
RewriteRule ^copyright/$ /index.php?module=home&action=copyright

RewriteRule ^error$ /index.php?module=error&action=error
RewriteRule ^error/$ /index.php?module=error&action=error

我应该如何编辑我的 .htaccess 文件来完成这个基本的重写?此外,对于我的 .htaccess 代码的任何其他反馈,我们将不胜感激。

提前致谢!

【问题讨论】:

  • 我这辈子从没见过这么浓密的 .htaccess 文件。是不是真的写了那么多乱七八糟的,不知道怎么添加这个简单的重写规则?
  • 我确实写过,哈哈,但是我遇到了重定向循环错误的问题。也许它太浓密了!大声笑有关如何使代码更高效的任何建议?

标签: php apache url .htaccess lamp


【解决方案1】:
RewriteRule ^/index.php$ /

但这没有意义,因为/ 很可能服务于index.php。 如果您的目录中有一个 index.php 文件并且不想将其作为默认文件,那么这是一个非常奇怪的配置!但当然可以...如果您在网络服务器配置中指定了不同的 DirectoryIndex 'es。

也许你想重定向 index.php/ ???

在这种情况下,您可以在您的配置中添加RedirectMatch,例如RedirectMatch 301 ^/index.php$ /,但我宁愿建议在您的php 文件中直接查看您的$_SERVER["REQUEST_URI];,但这是风格问题。如果可能的话,我个人喜欢尽可能多地控制我的应用程序,并且只有在更快或必要时才移动到服务器配置......

编辑:

在您的评论清除了您真正需要的内容之后,我可以给您两个解决方案。

Redirect / RedirectMatch 不起作用,因为您不能有条件地这样做,您可以在其中检查实际的请求 uri。此外,最终提供的 url 将用于重定向匹配。这意味着在 apache 通过 DirectoryIndex 指令重定向到 index.php 之后。 所以这些方法无法区分//index.php

所以你需要在你的 php 文件中做它是

版本 1:

查看$_SERVER['REQUEST_URI'] 是否以index.php 结尾,只有在实际请求(在浏览器栏中输入)时才会发生这种情况。在那里你可以使用header("Location: ...") 进行重定向。

版本 2:

使用mod rewrite 也可以进行重定向,并且可以根据条件进行。 包括用于演示目的的配置 (DirectoryIndex)。 您实际上只需要 RewriteCondRewriteRule 行。

DirectoryIndex index.php

RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ http://www.yourdomain.com/ [R=301,L]

我不确定是否可以离开您的域并只需输入/,您可以查一下。 仅当请求 url 实际上是 /index.php 时才会应用执行重定向的重写器。

【讨论】:

  • 我认为您是正确的,因为我希望从 index.php 重定向到 / 但是,使用 RedirectMatch 301 ^/index.php$ / 会导致重定向循环页面加载错误。有什么想法吗?
  • 我用的是第 2 版,效果很好。太感谢了!值得注意的是,(1) DirectoryIndex index.php 行似乎没有必要,(2) 用简单的 / 替换 yourdomain.com 似乎可行。
  • 很高兴能为您提供帮助。如果您以后想获得有关此帐户的帮助,我建议您接受并投票支持答案...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-03-04
  • 1970-01-01
  • 2016-06-26
  • 2011-03-23
  • 1970-01-01
  • 2015-05-21
  • 1970-01-01
相关资源
最近更新 更多