Forum Moderators: phranque

Message Too Old, No Replies

mod rewrite rule question about rewriting url's

not sure if its possible

         

surrealillusions

1:00 pm on Jan 19, 2009 (gmt 0)

10+ Year Member



Have read thru this excellent post,
[webmasterworld.com...]

i have a question about this part -

You cannot use mod_rewrite to change the address displayed in the browser... including to a more 'friendly' URL - The location displayed must either be requested or redirected to. The location does not need to contain any information, but must exist in a request.

If the location needs to exist, then how would you go about writing stuff for dynamically generated content, wouldnt you need to create the redirect folder/location as well or what?

For instance, if possible, I'd like to change a url from
example.com/folder/123foldername
to
example.com/folder/foldername

To make it more user and SE friendly, but to create a mod rewrite that would get rid of the numbers in front of the foldername for all sub-folders, instead of adding each one in turn to the .htaccess.

I've looked through some tutorials the library here, but its all a bit over my head at the moment in what to do, and how to go about it..although its slowly getting there, still confused about certain parts.

:)

coopster

3:11 pm on Jan 19, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



If the location needs to exist, then how would you go about writing stuff for dynamically generated content, wouldnt you need to create the redirect folder/location as well or what?

It can be a little confusing to wrap your head around at first, especially when you are thinking from the opposite side of this rewrite processing. In the quotation above -- let's say you have a "car" database driven web site with a table full of cars. Somebody wants to know more about a car. So you have a script called car.php (I'll use PHP in this example). Now, you create a script page in your domain's "car" directory that handles all the requests for car links. Perhaps on your site index page (home page) you have three links ...

<a href="/cars/chevrolet">Chevrolet</a> 
<a href="/cars/ford">Form</a>
<a href="/cars/gm">GM</a>

That is how you want the links to look in your request (in the browser address bar). Your car.php script will handle any GET data because your rewrite rules will redirect any request to the above links to your "car.php" script with the appropriate GET request variables appended.

g1smd

8:00 pm on Jan 19, 2009 (gmt 0)

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



Mod_Rewrite does not "make" URLs.

Mod_Rewrite takes the incoming request sent by the browser and internally translates that to get the content from a different location within the server - dynamic or static doesn't matter.

It is the URLs in links on the page that define the URL. So, you provide a link on your page. The format of the URL in that link *IS* the URL. That link when clicked will generate a request sent to the server. The server will either send a 404 error, or a redirect (301/302/307) response, or the rewrite will take that request, work out the real location inside the server, and get the content for you, and return it with a "200 OK" response.

.

URL on the page:

/cars/blue/left-handed

Rewrite Rule will take such a request and get the content from:

/products.php?product=cars&colour=blue&drive=left-handed

without revealing that is where the content really is.

surrealillusions

10:02 pm on Jan 19, 2009 (gmt 0)

10+ Year Member



Thanks..i think ive got it now. I'm guessing what i wanted in my original post is not possible?

Although ive found a way round it with the php script im using, still useful to know about apache mod rewrite anyhow.

:)

g1smd

10:15 pm on Jan 19, 2009 (gmt 0)

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



Yes of course it is possible.

Mod_Rewrite makes it possible.

You put the URL you want the world to see in the links on your pages.

You then write a rule that connects that request, when it arrives, to the real location of the content.

Of course, the real location is still accessible to the outside world, but no-one will know what that is if you don't tell them.

If they already know the real location, but you want them to stop referring to the content by that old URL, then you need a redirect so that when they ask for that old URL, the server sends a message that the user needs to ask for the new URL instead. That's exactly what a 301 redirect does.

.

So, the content is on your server at /bbbb but you want the user to use the URL www.example.com/aaaa to refer to it.

You link to /aaaa on the pages of your site. Links 'define' URLs.

You set up a redirect so that requests for /bbbb are sent a redirect to www.example.com/aaaa so the content cannot be directly reached at the /bbbb URL.

You also set up a redirect from non-www to www so that requests for non-www URLs are redirected to the www version. This prevents Duplicate Content issues.

You set up a rewrite so that requests for www.example.com/aaaa are handled such that the content is silently pulled from /bbbb inside the server.

The internal /bbbb filepath can be a dynamic filepath, or a static filepath. It doesn't matter. The rewrite makes this all possible.

Caterham

10:24 pm on Jan 19, 2009 (gmt 0)

10+ Year Member



still useful to know about apache

There are modules available which can modify the HTML source code, they are acting as an output filter.

A script should generate the desired URL in HTML links (<a href...>), generating something else and post-modifying the result (either in php (preg_replace in a buffer) or elsewhere) sounds like a bad programming strategy.