![](/img/trans.png)
[英]How to make http://example.com/test.php to http://example.com/index.php?do=test using .htaccess?
[英]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
我当前的.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代码的任何其他反馈将不胜感激。
提前致谢!
RewriteRule ^/index.php$ /
但这没有意义,因为/
可能会提供index.php
。 如果您的目录中有一个index.php文件,并且不想将其作为默认文件,则它的配置非常奇怪! 但当然可以...如果您在网络服务器配置中指定了不同的DirectoryIndex
。
也许您想将 index.php
重定向到/
???
在这种情况下,您可以像配置RedirectMatch 301 ^/index.php$ /
一样在您的配置中放置一个RedirectMatch
,但我宁愿建议您直接在$_SERVER["REQUEST_URI];
直接在您的php文件中这样做$_SERVER["REQUEST_URI];
但这是一个问题。风格,我个人希望尽可能在应用程序中拥有控制权,并且仅在速度更快或必要时才移至服务器配置...
编辑:
在您的评论清除了您的实际需求之后,我可以给您两个解决方案。
Redirect
/ RedirectMatch
无法正常工作,因为您不能有条件地这样做,您可以在其中检查实际的请求uri。 另外,最终提供的网址将用于重定向匹配。 这意味着在Apache通过DirectoryIndex
指令重定向到index.php
之后。 因此这些方法将无法分辨/
和/index.php
之间的区别。
所以您需要在您的php文件中执行此操作
版本1:
看看$_SERVER['REQUEST_URI']
以index.php
结尾,这只有在其实际请求(在浏览器栏中键入)时才会发生。 在那里,您可以使用header("Location: ...")
。
版本2:
使用mod rewrite
,它也能够进行重定向,并且可以在一定条件下进行重定向。 包括用于演示的configuratino( DirectoryIndex
)。 实际上,您只需要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]
我不确定是否可以离开您的域,只需键入/
,您就可以查找。 仅当请求url实际上是/index.php
应用重写器进行重定向。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.