简体   繁体   English

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

[英]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.php
to be rewritten to simply 改写成简单的
 http://example.com http://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. 实际上,您只需要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]

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.

相关问题 如何使用.htaccess将http://example.com/test.php设置为http://example.com/index.php?do=test? - How to make http://example.com/test.php to http://example.com/index.php?do=test using .htaccess? 如何将example.com/index.php#anchor的URL重写为example.com/anchor? - How do I rewrite the URL for example.com/index.php#anchor to example.com/anchor? Ubuntu-如何直接将example.com/opencart/index.php定向到http://example.com/和www.example.com? - Ubuntu - How direct example.com/opencart/index.php to http://example.com/ and www.example.com? http://example.com/fruits/apple我想重定向到http://example.com/pen/apple - http://example.com/fruits/apple I want to redirect to http://example.com/pen/apple 如何从这个example.com/index.php?lang=en更改我的多语言网址到这个example.com/en/? - How can I change my multilingual urls from this example.com/index.php?lang=en to this example.com/en/? 如何将example.com/index.php/lib更改为example.com/lib? - How to change example.com/index.php/lib to example.com/lib? 如何将example.com/forum/index.php重定向到example.com/forum/ - How to redirect example.com/forum/index.php to example.com/forum/ 如果网址中没有index.php,example.com / index.php中的首页无法加载 - Home page is not loading without index.php in url, example.com/index.php 将example.com/dashboard.php更改为example.com/index.php?action=dashboard - Change example.com/dashboard.php to example.com/index.php?action=dashboard 使用,htaccess将example.com?id=12重定向到example.com/index.php?id=12 - using ,htaccess redirect example.com?id=12 to example.com/index.php?id=12
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM