How to hide the URL change when using apache rewrite?

13,290

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.

Share:
13,290
cEMa
Author by

cEMa

Updated on August 01, 2022

Comments

  • cEMa
    cEMa over 1 year

    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.

  • Prix
    Prix over 10 years
    @anubhava thanks, I was considering that but that only applies if he actually want any query strings carried to the new URL format.
  • anubhava
    anubhava over 10 years
    Yes agreed but you never know these rewrite requirements. But yes OP didn't mention about any such requirement.
  • cEMa
    cEMa over 10 years
    @prix Thanks for such the speedy reply! As I was afraid of.. when using: RewriteBase / it causes the webservers to freak out when I try to run apachectl restart. Is it required for RewriteRule ^Page/(.*)/?$ /page.cfm?pagevar=$1 [QSA,NC,L] to work? Edit: the webserver throws RewriteBase: only valid in per-directory config files when running apachectl restart.
  • Prix
    Prix over 10 years
    @cEMa Like I have mentioned you don't need to place that on the virtualhost you place that on an .htaccess on the root folder of that domain.
  • cEMa
    cEMa over 10 years
    @Prix Is there a way to accomplish this NOT using a .htaccess
  • Prix
    Prix over 10 years
    Yes remove the rewritebase or place it inside a directory directive and place the / at the ^Page. You can only use RewriteBase inside <directory ...> on the conf as pointed out by your error message.
  • cEMa
    cEMa over 10 years
    @Prix Alright, I have gotten this error before, but I thought I had just used the wrong code. Using: RewriteRule ^/Page/(.*)/?$ /page.cfm?pagevar=$1 [QSA,NC,L] The url works, it stays the same. But the webpage looks like this: (postimg.org/image/l4rl1ojdv) to much text so had to do screenshot.. Any ideas what might cause this?? Seems like it's one thing after another!
  • Prix
    Prix over 10 years
    @cEMa you need to properly configure cfm to work and that is a complete different question and would go on serverfault.com
  • cEMa
    cEMa over 10 years
    @Prix, Thanks again for all of the continual support. It is very much appreciated!