[英]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?
On a LAMP server, I want the URL 在LAMP服务器上,我想要URL
http://example.com/index.phpto be rewritten to simply
http://example.comhttp://example.com
My current .htaccess file is as follows... 我当前的.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
How should I edit my .htaccess file to accomplish this basic rewrite? 我应该如何编辑.htaccess文件以完成此基本重写? Also, any other feedback with regard to my .htaccess code would be greatly appreciated.
此外,关于我的.htaccess代码的任何其他反馈将不胜感激。
Thanks in advance! 提前致谢!
RewriteRule ^/index.php$ /
but this doesn't make sense, as /
is likely to serve index.php
. 但这没有意义,因为
/
可能会提供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! 如果您的目录中有一个index.php文件,并且不想将其作为默认文件,则它的配置非常奇怪! but possible of course... if you have specified different
DirectoryIndex
'es in your webserver config. 但当然可以...如果您在网络服务器配置中指定了不同的
DirectoryIndex
。
perhaps you want to redirect index.php
to /
??? 也许您想将
index.php
重定向到/
???
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... 在这种情况下,您可以像配置
RedirectMatch 301 ^/index.php$ /
一样在您的配置中放置一个RedirectMatch
,但我宁愿建议您直接在$_SERVER["REQUEST_URI];
直接在您的php文件中这样做$_SERVER["REQUEST_URI];
但这是一个问题。风格,我个人希望尽可能在应用程序中拥有控制权,并且仅在速度更快或必要时才移至服务器配置...
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. Redirect
/ RedirectMatch
无法正常工作,因为您不能有条件地这样做,您可以在其中检查实际的请求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. 这意味着在Apache通过
DirectoryIndex
指令重定向到index.php
之后。 so those methods wont be able to tell the difference between /
and /index.php
. 因此这些方法将无法分辨
/
和/index.php
之间的区别。
so you need to do it either in your php file which is 所以您需要在您的php文件中执行此操作
version 1: 版本1:
see if $_SERVER['REQUEST_URI']
ends with index.php
, this will only happen if its actually requested (typed into the browser bar). 看看
$_SERVER['REQUEST_URI']
以index.php
结尾,这只有在其实际请求(在浏览器栏中键入)时才会发生。 there you can do a redreict using header("Location: ...")
. 在那里,您可以使用
header("Location: ...")
。
version 2: 版本2:
using mod rewrite
which is also able to do redirects and can do it on conditions. 使用
mod rewrite
,它也能够进行重定向,并且可以在一定条件下进行重定向。 in included the configuratino you have ( DirectoryIndex
) for demonstrating purposes. 包括用于演示的configuratino(
DirectoryIndex
)。 You actually only need the RewriteCond
and RewriteRule
line. 实际上,您只需要
RewriteCond
和RewriteRule
行。
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. 仅当请求url实际上是
/index.php
应用重写器进行重定向。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.