简体   繁体   中英

htaccess rewriterule directory redirect

I'm using htaccess to load files when users request a directory (if the file exists), So if you go to /login/ you load /login.php etc etc.

If the request is not index.php, and if the "map"(file) does not exists, it should load user.php with the map behind it, so /thomas/ should load user.php?dir=thomas

This is what I want, but I can't seem to get it to work, this is currently my htaccess, it works, but login/register/logout are also appended to user.php, and when I uncomment the line it will result in an infinite loop when (for example) the index is loaded.

RewriteEngine On

RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !index\.php [NC]
#RewriteRule ^(login|register|logout)(/|)$ $1.php [L,NC,QSA]
RewriteRule ^([^\?]*)$ user.php?dir=$1   [L,QSA]

Thanks in advance!

Edit: I changed the .htaccess so it checks if the php file exists, and if so it includes it. But now my folder /settings/ doesn't work anymore ;(

RewriteEngine On

RewriteBase /
## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1/ [R,L,NC]

# To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} ^(.*?)(/(.*)|)$
RewriteCond %{DOCUMENT_ROOT}$1.php -f
RewriteRule ^(.*?)(/(.*)|)$ $1.php?args=$2 [L,QSA]


RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\?]+)$ user.php?dir=$1   [L,QSA]

I want the same thing to happen like with the root. I want it to try for example, that when I go to /settings/modules it first checks if /settings/modules/index.php exist, if not, checks if /settings/modules.php exists, if not, check if settings.php exists, if it does, to include settings.php?args=modules but I don't know if this is possible :\\

I hope my intentions are clear x)

Try :

RewriteEngine On

RewriteBase /
# let existing file/dir pass through untouched#   
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
#####
RewriteRule ^(login|register|logout)/?$ $1.php [L,NC,QSA]
RewriteRule ^(.+)$ user.php?dir=$1   [L,QSA]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM