Forum Moderators: phranque

Message Too Old, No Replies

How to keep original URL in addressbar after rewrite

mod_rewrite changes the URL after a rewrite

         

thinkdj

7:04 am on Nov 12, 2008 (gmt 0)

10+ Year Member



hey guys, I have a small trouble with rewriting URLS

Please try visiting
http://example.com/files/3916/print
The address bar changes to
http://example.com/getfile.php?code=3916&filename=print

How do I make the rewrite rule so that the original URL is shown on the user's address bar ?

Existing rewrite rule :
RewriteRule ^files/([^&]+)/([^&]+)/?$ http://example.com/getfile.php?code=$1&filename=$2 [L]

(This worked fine in my previous host and the original URL was shown in the addr bar)

[edited by: jdMorgan at 5:13 pm (utc) on Nov. 12, 2008]
[edit reason] Please use example.com [/edit]

g1smd

12:05 pm on Nov 12, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



*** How do I make the rewrite rule so that the original URL is shown on the user's address bar ? ***

That is what a rewrite does, if properly coded. It takes the requested URL and silently fetches content from an internal filepath that it does not reveal back to the browser.

You included a domain name in the target URL and that automatically turns it into a 302 redirect, as documented in the Mod_Rewrite instructions. Remove the protocol and hostname and see what you get.

The links on your site also need to use the "short" format. It is is the links that define the URLs.

Ahead of your rewrite you also need a site-wide non-www to www (or vice-versa) 301 redirect to make sure that the rewrite responds to only one of those direct URL requests, not to both.

See also very recent posts as variants of this question have been asked three other times in the last two days alone.

jdMorgan

3:11 pm on Nov 12, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



See this thread [webmasterworld.com] for all the details.

Jim

thinkdj

4:41 pm on Nov 12, 2008 (gmt 0)

10+ Year Member



@g1smd
All I had to do was change
RewriteRule ^files/([^&]+)/([^&]+)/?$ http://example.com/getfile.php?code=$1&filename=$2 [L]
to
RewriteRule ^files/([^&]+)/([^&]+)/?$ getfile.php?code=$1&filename=$2 [L]
Thanks a million ..

@jdMorgan
Thanks dude.. For enlightening me about internal and external redirects ..

[edited by: jdMorgan at 5:13 pm (utc) on Nov. 12, 2008]
[edit reason] Example.com [/edit]

jdMorgan

5:09 pm on Nov 12, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Please do take g1smd's comment seriously: By rewriting both versions of the 'friendly' URL --with the trailing slash and without it-- you are setting yourself up for duplicate-content problems. In the best case, the PageRank/link-popularity of the pages can be 'split' beteen the slash and no-slash URLs, and you will have no control over which is displayed in the SERPs. In the worst case, you might invoke a penalty. Of course, none of us (Webmasters) know, but actual penalties are usually reserved for far worse cases, such as when hundreds of URLs resolve to the same content, but you would do well to read up on avoiding dup-content, and take steps to fully-canonicalize all URLs on your site, so that one and only one URL can be used to directly access any given page, and all other variations result in a 301 redirect to the correct canonical URL.

One thread on WebmasterWorld has a really good title -- "Duplicate Content -- Get it Right or Perish" -- And a search of WebmasterWorld for "duplicate content" will turn up many more informative threads.

In this case, you need to choose whether the URL should or should not end in a slash, and then externally redirect the non-preferred URL to the preferred one. This decision is correctly based on whether the resource is a "file" or a directory or directory index. In HTTP, only directory or directory-index URLs should end with a slash.

The same is true for http vs. https, for example.com vs. www.example.com vs. example.com. vs. example.com:80 vs. example.com.:80 (all of which are valid hostnames), for /index.html (or php, etc.) vs. "/", for various query string parameter orders, and for many other possible URL variations -- All of these should be detected by your server and redirected to the single correct URL.

This may sound like nit-picking, but it is part of "securing your rankings" against innocent linking errors and against non-so-innocent vulnerability exploits by or on behalf of your competitors...

Jim

g1smd

6:48 pm on Nov 12, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



We documented 28 different ways to make duplicate content problems on a website just a few months ago.

[webmasterworld.com...]