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$&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!
but this doesn’t make sense, as
/is likely to serveindex.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.phpto/???in this case you can put a
RedirectMatchin your config likeRedirectMatch 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/RedirectMatchwon’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 toindex.phpby apache via theDirectoryIndexdirective.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 withindex.php, this will only happen if its actually requested (typed into the browser bar). there you can do a redreict usingheader("Location: ...").version 2:
using
mod rewritewhich 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
RewriteCondandRewriteRuleline.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.phpapply the rewriterule which does the redirect.