在 LAMP 服务器上,我希望将 URL http://example.com/index.php 重写为 http://example.com。我该怎么做?

发布于 2024-09-24 15:16:43 字数 3505 浏览 0 评论 0原文

在 LAMP 服务器上,我想要 URL

http://example.com/index.php
to be rewritten to simply
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 代码的任何其他反馈将不胜感激。

提前致谢!

On a LAMP server, I want the URL

http://example.com/index.php

to be rewritten to simply

http://example.com

My current .htaccess file is as follows...

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
amp;page=$2
RewriteRule ^gallery/([^/\.]+)/([^/\.]+)/$ /index.php?module=gallery&action=album&album=$1
amp;page=$2
RewriteRule ^gallery/([^/\.]+)/([^/\.]+)/([^/\.]+)$ /index.php?module=gallery&action=item&album=$1
amp;page=$2&item=$3
RewriteRule ^gallery/([^/\.]+)/([^/\.]+)/([^/\.]+)/$ /index.php?module=gallery&action=item&album=$1
amp;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

How should I edit my .htaccess file to accomplish this basic rewrite? Also, any other feedback with regard to my .htaccess code would be greatly appreciated.

Thanks in advance!

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

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

发布评论

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

评论(1

相权↑美人 2024-10-01 15:16:43
RewriteRule ^/index.php$ /

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

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

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

编辑:

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

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

所以你需要在

版本 1 的 php 文件中执行此操作:

查看 $_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 时,才会应用执行重定向的重写规则。

RewriteRule ^/index.php$ /

but this doesn't make sense, as / is likely to serve index.php.
if you have an index.php file in your directory and don't want to serve it as default, its a very strange configuration! but possible of course... if you have specified different DirectoryIndex 'es in your webserver config.

perhaps you want to redirect index.php to / ???

in this case you can put a RedirectMatch in your config like RedirectMatch 301 ^/index.php$ / but i would rather recommend to do this in your php file directly looking at your $_SERVER["REQUEST_URI]; but this is a matter of style. i personally like to have as much control in my application if possible and only move to the server config if its faster or necessary...

EDIT:

after your comment which cleared what you actually need, I can give you two solutions.

Redirect / RedirectMatch won't work because you can't do it conditinally, where you can check for the actual request uri. additionally the finally served url will be used for redirectmatching. which means AFTER the redirect to index.php by apache via the DirectoryIndex directive.
so those methods wont be able to tell the difference between / and /index.php.

so you need to do it either in your php file which is

version 1:

see if $_SERVER['REQUEST_URI'] ends with index.php, this will only happen if its actually requested (typed into the browser bar). there you can do a redreict using header("Location: ...").

version 2:

using mod rewrite which is also able to do redirects and can do it on conditions.
in included the configuratino you have (DirectoryIndex) for demonstrating purposes.
You actually only need the RewriteCond and RewriteRule line.

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]

i am not sure wheter its possible to leave your domain and just type /, you can look that up.
this will only if the request url actually is /index.php apply the rewriterule which does the redirect.

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