Forum Moderators: phranque

Message Too Old, No Replies

How to make Redirected/RewriteRule'd URL nice in browser address?

Nice URL for browser/SEO from nasty redirected script url

         

puddleglum

5:55 pm on Mar 31, 2010 (gmt 0)

10+ Year Member



I want to make the URL in the address bar show the original url requested, not the redirected URL;

I have a script like;

[b]/db/db.pl?mode=products-widgets-whatever[/b]


but i like to use;

[b]/products/widgets/whatever[/b]


'cos it is easy to type and looks nicer in google et al. This is how we write calls to the script in most of our web pages.

We use either a redirect or rewriterule to send it to the script, e.g.

1) Redirect /Products/Widgets/ /db/db.pl?mode=
2) RewriteRule Products/Widgets/(.*)$ /db/db.pl?mode=$1 [nc]

Both of these work (the script gets the right params) but the address bar displays the redirected location;
http://wherever/db/db.pl?mode=blah

whereas i want it to say;
/Products/Widgets/blah

I really, really want it to say that.

g1smd

6:41 pm on Mar 31, 2010 (gmt 0)

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



This should work:

RewriteRule ^Products/Widgets/(.*)$ /db/db.pl?mode=$1 [L]


but you must link to the URLs that you want users to see and use.

Lose the [NC] flag or it will create a Duplicate Content issue.

You'll also need a preceding redirect such that if an external request is received for
example.com/db/db.pl?mode=<something>
the request is redirected to the correct URL.

puddleglum

8:02 pm on Mar 31, 2010 (gmt 0)

10+ Year Member



thx!

i can make the rule as you say, though i only need match the end of the string, but i can make it match the start with your ^. also add your [L]

however, result is the same.

just to be clear, i have no problem with redirection or regex, i want the browser address bar and reported url to be the nice one (first part of rule) not the script (second part) so /products/whatever not /script.pl?complex_params.

this is not a redirection problem, i am trying to display a different url to the redirected (actual) one

puddleglum

8:20 pm on Mar 31, 2010 (gmt 0)

10+ Year Member



ok, please correct me if i am being stupid and missing the point....

the redirection works, in actual fact the url i use is more complex than the above, but being from perl, regex and getting the script redirected to is NOT the problem, problem is what is displayed as the url.

example;

<a href="/Products/Widgets/Widget1">Widget 1</a><br>
<a href="/Products/Widgets/Widget2">Widget 2</a><br>
<a href="/Products/Sprocks/SprockX">Sprock X</a><br>

So these redirect perfect, using either redirect or rewriterule;

eg. Redirect /Products/Widgets/ /db/db.pl?mode=
or; RewriteRule Products/Sprocks/(.*)$ /db/db.pl?mode=$1 [L]

in either case, the resulting url will be;

/db/db.pl?mode=[correct params]

It is this resulting url that displays in the address bar and in google listings.

I don't want this. I want the original nice url /Products/Category/Item to be displayed.

g1smd

8:28 pm on Mar 31, 2010 (gmt 0)

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



That's what the code should do.

The Redirect rule redirects to a new URL, you don't want that.

The RewriteRule rewrites a URL request to an internal filepath. It should do so without changing the URL in the browser URL bar.

Make sure that all of your redirects use RewriteRule with [R=301,L] flags and that all redirects are listed before all of the rewrites.

in either case, the resulting url will be;

/db/db.pl?mode=<correct params>
No. The result will NOT be a URL. This should be an internal rewrite, not an external redirect.

puddleglum

8:36 pm on Mar 31, 2010 (gmt 0)

10+ Year Member



The RewriteRule rewrites a URL request to an internal filepath. It should do so without changing the URL in the browser URL bar.


well that is what i thought. so i remove all the redirects and just use a rewrite but the resulting displayed url is always the script path.

i even stripped down .htaccess so there is NOTHING else there, 'cept the engine on etc, and it redirects, but displays nasty url!

puddleglum

8:46 pm on Mar 31, 2010 (gmt 0)

10+ Year Member



ok i think i am using the term url bit flexibly. what i mean is address in browser bar and address on google result

jdMorgan

3:21 pm on Apr 1, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You are confusing redirects and rewrites and you are confusing URLs with filepaths.

A redirect is an HTTP response from your server to a client request. It says "What you asked for has moved. Ask for it again using this new (provided) URL." This response terminates the current HTTP request/response transaction, and it is up to the client to invoke a new one. The client may (it is not required to) repeat its request, using the new URL provided in the server's redirect response. If it does so, it will update its address bar to reflect this new URL. A redirect is a URL-to-URL translation.

A rewrite is an operation that takes place solely within your server, and solely within the context of the current HTTP request. It simply "maps" a client-requested URL to a (non-default) server filepath. The client is totally unaware of this operation, if it is correctly implemented. A rewrite is a URL-to-server-filepath translation.

URLs are defined by HTML pages and are used "out on the Web." Filepaths are defined and used inside servers. These are not equivalent things, and in fact, need not even have any resemblance to each other. It is only the action of the server that "connects" URLs to filepaths.

Link to the friendly/pretty URL from your HTML pages.

Then internally rewrite --do not redirect-- requests for these friendly/pretty URLs to your script filepath.

Once that is working, add another rule that detects direct client requests for the old/unfriendly/dynamic URLs, and redirects them to the appropriate new/friendly/pretty/static URLs.

This last step will only be possible if the old URLs contain all necessary 'text' to construct the related new URLs. If this is not the case, then an alternative is to detect the old/unfriendly/dynamic URLs and rewrite them, either to a new script, or to your existing script, but with an additional name/value pair in the query string that says, "this old/unfriendly/dynamic URL request needs to be redirected." The script can then look up the new/friendly/pretty/static URL that corresponds to the client-requested URL in your database, and generate a 301-Moved Permanently redirect response.

The "direct client request" proviso is required in order to prevent an infinite rewrite/redirect loop. You must check the the dynamic URL is being requested directly by the HTTP client, and is not being requested as a result of your internal friendly-URL-to-script rewrite rule. This can be accomplished by using a RewriteCond to check the URL-path in the client request line stored in the server variable %{THE_REQUEST} to be sure it is dynamic and matches that 'seen' by the RewriteRule, or by checking the %{REDIRECT_STATUS} variable for a blank value. Several examples of both of these approaches are available in previous threads here (search for "RewriteCond" plus the variable names).

The thread entitled "Changing Dynamic URLs to Static URLs [webmasterworld.com]" in our Apache Forum Library discusses the entire process, and may prove useful.

Also, be sure to completely-flush (that is, delete) your browser cache before testing any newly-uploaded server-side code -- including .htaccess files, scripts, etc. Otherwise, your browser may not send any new requests to your server, but instead will show you stale cached page contents and server responses.

Jim