开发者

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?

开发者 https://www.devze.com 2023-01-17 07:54 出处:网络
On a LAMP server, I want the URL http://example.com/index.php to be rewritten to simply http://example.com

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/开发者_StackOverflow社区([^/\.]+)$ /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? Also, any other feedback with regard to my .htaccess code would be greatly appreciated.

Thanks in advance!


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号