Forum Moderators: phranque

Message Too Old, No Replies

New to mod rewrite, trying to rewrite dynamic URL

need to replace directory name

         

dpinion

2:11 pm on Jun 17, 2009 (gmt 0)

10+ Year Member



Greetings All,
I am currently reading over the mod_rewrite docs at apache's site, but while I ingest it, I have a question. My company uses Joomla for its content, and the name of a category managed to get switched. While it was switched, of course, google (and others) indexed it. So now I have 404s showing up for those pages. I would like to 301 those pages to the correct current content, but not sure how. Basically what I have is like this:

(incorrect)
boards/pci-express-backplanes?view=product&product_id=3311&layout=backplane

and what I need is:
boards/backplanes?view=product&product_id=3311&layout=backplane

so basically I need to replace "pci-express-backplanes" with "backplanes" in these URLs (there are quite a few).

Thanks for any assistance!

jdMorgan

2:22 pm on Jun 17, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



See this thread [webmasterworld.com] from earlier today to get started.

Jim

dpinion

2:35 pm on Jun 17, 2009 (gmt 0)

10+ Year Member



Thanks for the link Jim,
However I am a bit confused. Trying to pull the other thread and mine together a bit, would I have something like:

RewriteCond %{QUERY_STRING} ^\/pci-express-backplanes\/view=
RewriteRule ^$ \/backplanes\/?view= [R=301,L]

jdMorgan

2:54 pm on Jun 17, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You are still mixing URL-paths and query strings. They are separate things.

Assuming that this code goes into your example.com/.htaccess file:


RewriteCond %{QUERY_STRING} ^view=product&product_id=[0-9]+&layout=backplane
RewriteRule ^boards/pci-express-backplanes$ http://example.com/boards/backplanes [R=301,L]

Note that the query string is passed through the rule unchanged by default, and so will be re-attached to the new URL.

Do not use character-escaping except within regular-expressions patterns, and then only when actually required. The characters which need to be escaped are those which have special meaning to the regular-expressions matching engine, and those are listed in the Apache mod_rewrite documentation. Note that these escaping rules (POSIX 1003.2) differ from those used in PERL, PHP, etc., which use PCRE.

Jim

dpinion

3:15 pm on Jun 17, 2009 (gmt 0)

10+ Year Member



Thanks Jim,
I will give that a go. And will look into the character escaping. I don't seem to have a problem with my other URLs that have been escaped. Is it something that would cause me problems with search engines?

jdMorgan

4:29 pm on Jun 17, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's a waste of filespace, memory, and CPU time, makes your code hard to read, and serves as a "non-optimal" example if copied. As a long time coder (35+ years), I believe it is best to "do it exactly the way the documentation says it should be done," and I don't let my personal preferences sway me.

There is also the possibility that some new version of the code interpreter (e.g. mod_rewrite) might be released in the future -- One that is coded according to the documentation, but does not allow the same liberties to be taken with the coding style as did the older version. If that happened, your code would break. You'd have to go back and edit the code on your server -- or perhaps on dozens, hundreds, or even thousands of servers...

As a result, the code I post here is as close to being "perfect" according to the documentation as I can make it -- I take no liberties with 'style' of any kind.

Jim

dpinion

4:53 pm on Jun 17, 2009 (gmt 0)

10+ Year Member



Being new to this arena, I don't have any personal preferences to sway me. :) But I do want to understand how and why things should be done, so thank you for your explanation.

As mentioned earlier, I am reviewing the mod_rewrite docs online and hope to gain understanding.

One thing that I do find amusing is that I was using the redirect tool provided through my host's cpanel and when I opened up the htaccess it generated, lo and behold, there were escape characters everywhere. So as you say, if they do decide to take a stricter stance on it, lots of people could have lots of issues.

jdMorgan

5:07 pm on Jun 17, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Control panels often generate "simply awful" code.
The code distributed with WordPress and especially Joomla is really bad (it does many, many unnecessary and inefficient filesystem checks).
The code posted in many forums is also poor.

And these are the reasons that I'm such a stickler for adhering to the documented syntax... I don't want to be responsible for spreading badly-written or inefficiently-coded rules.

I also studiously ignore all control panel features that I can do for myself. I've even found some of those features that once done, cannot be un-done -- The activation of custom .shtml error documents being one widespread example. Once turned on, they can't be removed in many control panels I've seen.

Jim