简体   繁体   中英

How to force a mod_rewrite RewriteRule to display as rewritten even if the un-rewritten url is entered

is it possible to make it so that url rewrites are forced?

Here is an example of what works, http://localhost/home/ goes to http://localhost/index.php?page=home but the url stays the same to the user.

What I can't get to work is forcing http://localhost/index.php?page=home to display as http://localhost/home/ .

I don't know if I'm using the right terminology, but I want it so that if you type the non rewritten url, I want the rewritten url to appear in the user's browser.

Here is what I have working so far:

RewriteEngine on
Options +FollowSymLinks

RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond $1 !^(index\.php)
RewriteRule ^([a-zA-Z0-9_]*)/([a-zA-Z0-9_]*)/([a-zA-Z0-9_]*)/?$ index.php?page=$1&a=$2&b=$3 [L,NC,QSA]

RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond $1 !^(index\.php)
RewriteRule ^([a-zA-Z0-9_]*)/([a-zA-Z0-9_]*)$ index.php?page=$1&a=$2 [L,NC,QSA]

RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond $1 !^(index\.php)
RewriteRule ^([a-zA-Z0-9_]*)$ index.php?page=$1 [L,NC,QSA]

That's what the [R] flag is for. If you have a correctly-working rewrite, you'll want to add [R] to your other flags at the end of your RewriteRule.

Try adding some code to your index.php.

Look at the $_SERVER['SCRIPT_URL'] variable for your answer. When you enter "localhost/index.php?page=home" in your browser, SCRIPT_URL should equal '/index.php'. If you enter "localhost/home", SCRIPT_URL should equal '/home'. You can do a check for this in your PHP script and redirect appropriately from there.

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