htaccess rewriterule and directoryindex

2019-07-29 07:23发布

问题:

I'm having issues with my rewriterule and directory index

DirectoryIndex invite.php about.php account.php

RewriteRule ^/?([a-z0-9_-]+)/?$ /user.php?u=$1 [L]

Lets say about.php is in the about folder. When i go to www.url.com/about/about.php, it works fine. But when I go to www.url.com/about/, it goes to my user.php page. When I go to www.url.com/about, (without ending slash), it goes to www.url/about/?u=about

Can anyone help me?

At the same time, I'm trying to set up where user can go to www.url.com/andrewliu instead of www.url.com/user.php?u=andrewliu

I guess I need to somehow fix my ending slash and the redirect?

Thanks!

EDIT

I have this

DirectoryIndex invite.php about.php account.php

RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^(.*)$ $1.php
RewriteRule ^/?([a-z0-9_-]+)/?$ /user.php?u=$1 [L]
RewriteCond %{QUERY_STRING} !^b= [NC]
RewriteRule ^(business)/(.*)$ /$1/index.php?b=$2 [L,NC,QSA]

回答1:

You need to add a check to see whether the request is for a file or directory that exists. You need to add these conditions before your rule:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?([a-z0-9_-]+)/?$ /user.php?u=$1 [L]

This makes it so when you go to /about/, since it's a directory that exists, this rule won't be applied. But if you go to /andrewliu, it will be as long as that's not a file or directory.