8

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.

cEMa
  • 85
  • 1
  • 1
  • 6
  • [Don't forget to take a look on how to reward users that have helped you, improve your question when needed and other useful tips on how to best use this website.](http://stackoverflow.com/about) – Prix Sep 06 '13 at 16:31

2 Answers2

7

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.

Prix
  • 19,417
  • 15
  • 73
  • 132
  • @anubhava thanks, I was considering that but that only applies if he actually want any query strings carried to the new URL format. – Prix Sep 06 '13 at 16:55
  • Yes agreed but you never know these rewrite requirements. But yes OP didn't mention about any such requirement. – anubhava Sep 06 '13 at 17:05
  • @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`. – cEMa Sep 06 '13 at 18:39
  • @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. – Prix Sep 06 '13 at 18:40
  • @Prix Is there a way to accomplish this NOT using a .htaccess – cEMa Sep 06 '13 at 18:43
  • Yes remove the rewritebase or place it inside a directory directive and place the `/` at the `^Page`. You can only use `RewriteBase` inside `` on the conf as pointed out by your error message. – Prix Sep 06 '13 at 19:16
  • @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: (http://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! – cEMa Sep 06 '13 at 23:37
  • @cEMa you need to properly configure cfm to work and that is a complete different question and would go on http://serverfault.com/ – Prix Sep 07 '13 at 11:09
  • @Prix, Thanks again for all of the continual support. It is very much appreciated! – cEMa Sep 07 '13 at 16:42
0

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]
Jon Lin
  • 142,182
  • 29
  • 220
  • 220