简体   繁体   中英

How to get all possible URLs using .htaccess into one file?

I would like to have an index.php file that controls who can access to each file accessed by the URL, so I decided to use the .htaccess file for that.

I tried both rules:

RewriteRule ^(.*)$ ?get=$1

But as it seems that it makes a loop (as anubhava said) I also tried:

RewriteRule ^[^\?](.*)$ ?get=$1

But in both cases accessing to ?get=hello I got "500 Internal Server Error" instead of getting the hello value.

Why? How could I get the full url into one file even if it's a reference to an existing file?

Yes that is correct behavior. You are getting 500 internal error due to looping. Since URI pattern before and after rewrite is matching .* .

Remember that mod_rewrite keep executing your rules as long as there is a matching rule.

To overcome this looping issue you need RewriteCond :

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ ?get=$1 [L,QSA]

Which basically means that execute the rule only if request is not for a valid file or directory.

EDIT: As per your edited question, use this rule:

Options -MultiViews
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.*)$ ?get=$1 [L,QSA]

Which basically means to rewrite only if %{ENV:REDIRECT_STATUS} variable is not set. ( This variable is set to 200 after first successful rewrite by mod_rewrite module ).

EDIT (by axelbrz): I also added the Options -MultiViews line by anubhava to ignore the content negotiation module of apache that ruins the expected behaviour. Another possibility to ignore it is by disabling it running: sudo a2dismod negotiation and restarting apache.

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