Forum Moderators: phranque

Message Too Old, No Replies

Rewrite Rule: Left side of the equation

404 error

         

guru5571

5:14 pm on Sep 15, 2005 (gmt 0)

10+ Year Member



I know mod_rewrites have been explained ad infinitum. I have read the library and a lot of posts and done my own research but still can't get it to work. I know this forum has some of the sharpest knives in drawer, so to speak, so I'm asking for help.

I want to change this

[fakedomain.com...]

into this

[fakedomain.com...]

I realize there is no page name (ex. widgets.php) preceding the parameters, this is how my pages are set up for other reasons.

Here is an example of an .htaccess file that will get me a 404 errror


RewriteEngine on
RewriteBase /

RewriteCond %{QUERY_STRING} ^widget_id=(.+)\&widget_color=(.+)\&widget_size=(.+)$

RewriteRule ^$ [fakedomain.com...] [R=301,L]

Here is what this does in addition to the 404 error.
It gives me a URL like this:

[fakedomain.com...]

This is the URL I want with all the original parameters I don't want appended onto the end. And of course the page won't show up, I just get the 404.

So how do I get the appended info off the URL and the page to show up in the browser. Please help.

P.S. -- I don't know how to disable those URLs from being active links.

jd01

7:36 pm on Sep 15, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



To remove the original QUERY_STRING you will need to append a blank one:

RewriteCond %{QUERY_STRING} ^widget_id=(.+)\&widget_color=(.+)\&widget_size=(.+)$
RewriteRule ^$ http://www.fakedomain.com/%1/%2/%3/? [R=301,L]

That will not solve your 404 problem though...

To solve that problem, you will either have to change your links to the static page, and then serve the dynamic content to those locations like this:

RewriteRule ^([^/]+)/([^/]+)/([^/]+)/$ /?your_variable=$1&another=$2&last=$3 [L]

Or, you will need to use THE_REQUEST instead of QUERY_STRING and rewrite original requests to the static location, then access the dynamic location with an internal rewrite (see rule above) and serve that to the static location.

I do not recommend this way, unless you change your links at the same time... If you do this and do not change your links, every link on your site will point to a page that is permanently moved. (Somehow this does not seem like a very good idea.)

That would look something like this:

RewriteCond %{THE_REQUEST} \?widget_id=(.+)\&widget_color=(.+)\&widget_size=(.+)
RewriteRule ^$ http://www.fakedomain.com/%1/%2/%3/? [R=301,L]

RewriteRule ^([^/]+)/([^/]+)/([^/]+)/$ /?your_variable=$1&another=$2&last=$3 [L]

Hope this helps.

Justin

Edit: Oops! copy and paste error -- THE_REQUEST should not have had a hard ending ($) on it.

guru5571

8:18 pm on Sep 15, 2005 (gmt 0)

10+ Year Member



Thanks for your reply jd01. I was hoping to get a response from you in particular.

I tried this:

RewriteCond %{QUERY_STRING} ^widget_id=(.+)\&widget_color=(.+)\&widget_size=(.+)$

RewriteRule ^$ [fakedomain.com...] [R=301,L]

Which is basically adding a '?' to the end of my right side. As you predicted, this stopped the parameters from appending. Also as you predicted this did not solve my 404 problem.

So then I tried this:

RewriteRule ^([^/]+)/([^/]+)/([^/]+)/$ /?widget_id=$1&widget_color=$2&widget_size=$3 [L]

All this seems to do is display the original URL. So it makes no change at all.

I know I must be missing something here. Continued help would be much appreciated.

jd01

11:59 pm on Sep 15, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Glad I could help =)

If I am understanding what you would like, you will need to use this:

RewriteCond %{THE_REQUEST} \?widget_id=(.+)&widget_color=(.+)&widget_size=(.+)
RewriteRule ^$ http://www.fakedomain.com/%1/%2/%3/? [R=301,L]

RewriteRule ^([^/]+)/([^/]+)/([^/]+)/$ /?your_variable=$1&another=$2&last=$3 [L]

That will redirect the ?widget page to the static URL and then serve the information from the ?widget page... (I still strongly recommend you change the links to point to the static URL if you use this solition.)

If this is not what you are looking for, please give some more specifics.

Justin

** removed \'s from the Condition...

guru5571

12:20 am on Sep 16, 2005 (gmt 0)

10+ Year Member



Justin, thanks for the continued help. I've been reading over your mod_rewrite website, and it is an excellent resource.

(I still strongly recommend you change the links to point to the static URL if you use this solition.)

If you mean all the links on my pages. Most of them are generated dynamically, so I'm not sure how well this would work for me.

Sorry to be so dense. I'm not sure if I understand this correctly.

jdMorgan

12:51 am on Sep 16, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The links on your pages are the links that robots will fetch, rank, and list in search results. So any chenges to your 'public' URLs must be made there -- on your pages.

Mod_rewrite is then used to 'associate' those new public URLs with the correct path inside your server. That is, it translates your new 'friendly' URLs back into the form needed to call your script.

The concept may be difficult at first, but the key is to realize that URLs and filepaths are two entirely different 'views' of site resources, and they need to have no common elements whatsoever. A URL doesn't need to
contain any part of a filename, and vice-versa.

A simple demonstration of that would be a rewrite such as:


RewriteRule ^foo\.html$ ^main.php?page=foo [L]

So you request "foo.html" from the server, and mod_rewrite passes the request to a file called "main.php" with "foo" as a parameter. Main.php then takes the parameter and creates the correct "foo" page.

It is critical to understand that mod_rewrite acts on URLs requested from your server *before* any scripts are invoked and before any content is returned; It is a front-end process that modifies incoming reequests, not outgoing content.

You can either edit the URLs in your database, or you can add code in your script to do a php preg_replace (or string replace) to 'rearrange' the information in each link to make the URL 'friendly'. Note that I said re-arrange; You must be sure to preserve all information that mod_rewrite will need to rebuild the query to activate your script.

I hope that clarifies things.

Jim

guru5571

4:49 pm on Sep 16, 2005 (gmt 0)

10+ Year Member



Thanks for the detailed explanation Jim. This sheds some light on a few things I didn't realize. I now have quite a bit to chew on, so I'll have to do some experimenting and report back when I make some progress.

I'm sure lots of other people will find the details of this thread valuable. My hat goes off to you guys. This forum has the best expert answers of any place on the web, bar-none.

guru5571

9:24 pm on Sep 25, 2005 (gmt 0)

10+ Year Member



Thanks for all the help Justin and Jim. It took a lot of pounding to force those ideas into my head, but I finally did it... And it turned out to be very successful.

I guess the hardest part for me was coming to terms with having to rewite all the links in my pages. That was a lot of work because I have a lot of templates from a big sloppy site that has grown over time without an overall hierarchy or structure.

So I think I was wanting mod_rewrite to do the impossible. As in automatically forwarding all my old links new rewritten URLs. Of course mod_rewrite doesn't work that way and this is impossible.

So people hoping for this should reread this from the library. Not only reread it, but really understand it.


1. What mod_rewrite does not do:
A. Create anything.
B. Write a 'fake' URL in a browser.
C. Change anything, except the location the request is delivered from, or the location of the information delivered to the page requested.

I read it, but obviously was in denial about it. When I finally began to figure things out, my biggest chore was to change all the links in my pages. The mod_rewrite regular expressions turned out to be the easy part.

Most of my work was turning php URLs with up to a dozen parameters into nicer looking ones and making the whole site appear to have an overall and consistent directory structure. As I was going through all my links I found that I had a lot of inconsistency in the order of my parameters. This probably made a lot of my content appear to be duplicate content to the SEs. All this has since been remedied over the past week.

I will also say, that just in the past few days, every major search engine has spidered much more of my site than ever before. I think the haphazard links I had before probably caused a lot of confusion to SE spiders and as a consequence they did not spider all of my site.

So I'm optimistic that mod_rewrite will help my SE rankings over time and the new URLs will give a good indication of each pages content and have it all ordered in a logical hierarchy.

Thanks again guys for your help on this.

jdMorgan

4:21 am on Sep 26, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks very much for the feedback. It will be especially helpful to readers who are considering this kind of rewrite project. We discuss the 'how' part of this subject quite a lot, but rarely discuss the 'why' part.

I liked your comment about -paraphrasing here- 'a big sloppy site with no overall architectural plan.' Sometimes it takes a cleanup project of this scope to drive home the need to have a plan that allows for growth in addition to current needs. In fairness, I think it's necessary to really mess things up a few times before it's even possible to learn how to organize site URLs. Even allowing for many variations in 'personal style' of URL layout, there are certainly many more bad ways to do it than there are good. So the trick is to at least avoid the bad ways until a few good ways are discovered.

You're probably feeling worn out from this job, but I think that you *will* be quite pleased with the results. As WebmasterWorld itself demonstrates, a dynamic site that appears to be static can be quite well-indexed and well-ranked in search.

Thanks again for the feedback on this project!

Jim