Forum Moderators: phranque

Message Too Old, No Replies

dynamic url instead of static url in the address bar after mod rewrite

still showing dynamic url instead of static url in the address bar

         

gablu

1:33 pm on May 4, 2010 (gmt 0)

10+ Year Member



Hi,

Please help me to solve the problem stated below.

I have done all the required steps to change the dynamic url to static url through mod_rewrite in .htaccess file. But whenever I'm clicking on a particular link the address bar still showing me the dynamic url instead of the static url though it is sending me to the correct page.

Example:
I've created an internal link in the static format, like, "http://www.example.com/casestudy/34" in my web page.

And I've written in the .htaccess file the following code:

Options +FollowSymLinks 
RewriteEngine On
RewriteBase /

RewriteRule ^casestudy/([^-]+) /casestudy_details.php?id=$1 [R=301,QSA,L]


Now whenever I'm clicking on the static link the server send me to the correct page but in the address bar it shows me "http://www.example.com/casestudy_details.php?id=34" instead of "http://www.example.com/casestudy/34".

Why this is happening? Can anybody tell me what to do to correct this problem?

Thanks.

jdMorgan

3:37 pm on May 4, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi gablu, and welcome to WebmasterWorld!

You've only done step two of a two-step process...

You need to edit your pages --or edit the script or database used to produce your pages-- to correct your URLs.

The links on your Web pages define your URLs, and mod_rewrite cannot "change" them.

Once you have edited the links on your pages, the rule you posted (with the modification & correction shown below) above will "re-connect" requests for the new URLs to the same server filepaths that used to be used to serve requests for your old URLs.

If your "case studies" are always numeric, and if there is never any additional information in the URL following the case study number, then I would suggest changing your pattern to be more-specific:

RewriteRule ^casestudy/([0-9]+)$ /casestudy_details.php?id=$1 [QSA,L]

Note also that the [R] flag has been removed, as this is an internal URL-to-filepath rewrite, not a redirect.

Once this is working there is an optional third step, which can be used to speed up search engines' removal of your old URLS and replacement with you new URLs. But that is all you can use it to do, it does not "change the URLs." We can discuss that once you get the first two steps done.

For more information on this subject, see the thread titled Changing Dynamic URLs to Static URLs [webmasterworld.com] in our Apache Forum Library.

Jim

g1smd

10:23 pm on May 4, 2010 (gmt 0)

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



In the initial code you needed an internal rewrite to connect an incoming URL request to the server's internal filepath to fetch the content.

By including the R=301 flag you instead coded an external redirect. Remove the R=301 flag and note the extra efficiencies as coded in the post above.

gablu

10:23 am on May 5, 2010 (gmt 0)

10+ Year Member



Hi Jim & g1smd,

Thanks for your quick reply.

Actually I have already read the content "Changing Dynamic URLs to Static URLs" which is written by Jim.

I think, I have followed all the steps properly.

Please look at my previous posting. I have mentioned that, "I've created an internal link in the static format, like, "http://www.example.com/casestudy/34" in my web page.".

I think Jim, you have mentioned this as the first step.
i.e. creation of static link in the pages first. In my page all the case study links are written as "http://www.example.com/casestudy/34", "http://www.example.com/casestudy/33" etc.
Hope this is the first step you are talking about.

Now, the second step. I have already modified the mod_rewrite rule as per your suggestion.

But the most unfortunate thing is still the problem is occuring.

If you want then I can provide you the original link.

Thanks.

mack

10:29 am on May 5, 2010 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Part 1 will enable the friendly urls, but you need to make sure that the links on your pages are pointing to them. For example if your dynamic page is example.com?q=1234 and your preferred url is example.com/foo then you need to make sure your links are pointing to the correct location.

Mack.

gablu

10:43 am on May 5, 2010 (gmt 0)

10+ Year Member



Mack

The static links are exactly the same what I need in the dynamic one.

Example:
Static link: http://www.example.com/casestudy/33
Dynamic link: http://www.example.com/casestudy_details.php?id=33

Hope this is the right thing.

g1smd

11:13 am on May 5, 2010 (gmt 0)

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



Let's see your current code.

gablu

11:27 am on May 5, 2010 (gmt 0)

10+ Year Member



This is the actual link:
http://www.example.co.uk/margolistest/newsite/casestudies.php

Just check out the individual case study link - "READ the case study".

[edited by: jdMorgan at 12:42 pm (utc) on May 5, 2010]
[edit reason] example.co.uk [/edit]

g1smd

11:33 am on May 5, 2010 (gmt 0)

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



Yes, you have coded a redirect, when what you need is a rewrite.

gablu

11:55 am on May 5, 2010 (gmt 0)

10+ Year Member



Yes that's right.

Here is the redirect code written in the .htaccess file:

Options +FollowSymLinks
RewriteEngine On
RewriteBase /

RewriteRule ^casestudy/([0-9]+)$ http://www.example.co.uk/margolistest/newsite/casestudy_details.php?id=$1 [QSA, L]

[edited by: jdMorgan at 12:41 pm (utc) on May 5, 2010]
[edit reason] example.co.uk [/edit]

g1smd

12:36 pm on May 5, 2010 (gmt 0)

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



Remove the domain name to change this to a rewrite.

You need a rewrite here, not a 302 redirect.

gablu

12:55 pm on May 5, 2010 (gmt 0)

10+ Year Member



Could you please let me know what exactly I have to do.

Please provide me the exact code as if I remove the domain name then it is showing me the 404 error.

The edited code is :
Options +FollowSymLinks
RewriteEngine On
RewriteBase /

RewriteRule ^casestudy/([0-9]+)$ /casestudy_details.php?id=$1 [QSA,L]

jdMorgan

12:56 pm on May 5, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Understand that a redirect makes the dynamic URL visible to clients -- browsers and robots. So using a redirect destroys any advantage of trying to use 'search-engine-friendly' URLs in the first place, because search engines will index the dynamic URL and list it in the search results.

The form of an internal rewrite in this specific case would be

RewriteRule ^casestudy/([0-9]+)$ /margolistest/newsite/casestudy_details.php?id=$1 [QSA,L]

This "connects" the requested URL www.example.co.uk/casestudy/123 to the server filepath /margolistest/newsite/casestudy_details.php?id=123

What's the difference between external redirects and internal rewrites? [webmasterworld.com]

Jim

gablu

1:11 pm on May 5, 2010 (gmt 0)

10+ Year Member



Jim, it's a great help to understand the difference of external redirects and internal rewrites. I'm not aware of that as I am novice in this field.

In my current situation, after the internal rewrite the browser is showing me the 404 error. Why this is happening?

I have already posted the current code, which I'm using, above in my last posting.

g1smd

1:13 pm on May 5, 2010 (gmt 0)

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



The code you need for the internal rewrite is:

RewriteRule ^casestudy/([0-9]+)$ /margolistest/newsite/casestudy_details.php?id=$1 [QSA,L]


On the left is the requested URL path.

On the right is a reference to the location inside the server that will serve the content.

gablu

1:21 pm on May 5, 2010 (gmt 0)

10+ Year Member



Hard luck.

It's still showing me the same 404 error.

g1smd

1:54 pm on May 5, 2010 (gmt 0)

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



Did you flush the browser cache before each test?

Is this code going in an .htaccess file in the root of the site, or in a folder?

You'll need to adjust the paths mentioned in the rewrite to suit your situation.

It is a trivial rule to get working - but it will only work if the patterns are exactly right.

As coded above it belongs in the
/margolistest/newsite/.htaccess
file.

gablu

2:18 pm on May 5, 2010 (gmt 0)

10+ Year Member



I think I have solved the problem. I was creating a wrong static link with an extra "/" at the end. Now the static page is properly rewriting to the dynamic page.

Thank you Jim. Thank you g1smd. You both helped me a lot to solve this problem.

But now I'm facing a new problem. The page is showing without any images or css.

I'm using image link as : "images/logo.gif" or css link as "css/general.css".

Do you have any idea, how to overcome this problem?

g1smd

2:24 pm on May 5, 2010 (gmt 0)

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



The browser evaluates relative URLs found in links using the folder level of the currently requested HTML page.

Always link to images, and to CSS and JS files, using a server relative link that begins with a slash and specifies the
href="[b]/[/b]full-path/to-the/file"
.