How to hide the URL change when using apache rewri

2019-04-24 23:36发布

问题:

How do I hide the URL change when using an apache rewrite? I have searched for hours on this issue and have decided to come here to find out the answer. So any help/clues would be greatly appreciated!

Right now I am using:

RewriteRule ^/Page/(.*)$ http://domain.com/page.cfm?pagevar=$1 [NC,L]

The problem with that is, when you go navigate to http://domain.com/Page/abc123 it works. BUT, it changes the browser url to http://domain.com/page.cfm?pagevar=abc123,

I want it to perform that same action, but show http://domain.com/Page/abc123 as the url.

Please, any insight on this would be very appreciated! Thanks again.

回答1:

First rule will redirect your ugly URL to the pretty URL format.

Second rule will internally redirect it back for the user will not see the ugly URL.

Options +FollowSymLinks -MultiViews

RewriteEngine On
RewriteBase /

# Redirect /page.cfm?pagevar=abc123 to /Page/abc123
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+page\.cfm\?pagevar=([^&\s]+) [NC]
RewriteRule ^ /Page/%1? [R=301,L]

# Internally forward /Page/abc123 to /page.cfm?pagevar=abc123
RewriteRule ^Page/(.*)/?$ /page.cfm?pagevar=$1 [QSA,NC,L]

The above rules are to be used on .htaccess files and assumes page.cfm is on the root of your domain folder along with the .htaccess file.

Like your examples proposes.



回答2:

You need to get rid of the http://domain.com part of the rule's target. When you have that, it implicity redirects the browser instead of internally rewriting:

RewriteRule ^/Page/(.*)$ /page.cfm?pagevar=$1 [NC,L]