Forum Moderators: phranque

Message Too Old, No Replies

.htaccess/RewriteRule help?

My syntax is echoing extra segments!

         

evarose

7:52 pm on Jan 23, 2008 (gmt 0)

10+ Year Member



I've got an .htaccess file set up to redirect my (complex) category structure to nice, google-friendly, real-word URLs. However, I'm having a problem with a trailing wildcard -- anyone got any .htaccess syntax help to lend? I'm just brailling my way around at this.

My current syntax is:


RewriteRule ^(dir1¦dir2¦dir3)/state\/city/?(.*)$ $1/template/C123/$2

My hope is that requests for


http://www.example.com/dir1/state/city/term

will rewrite to


http://www.example.com/template/C123/term

However, I'm getting extra segments appended to my URL. For example,


http://www.example.com/dir1/state/city/term

is rewriting as


http://www.example.com/template/C123/term/state/city/term

I also need this to work so that


http://www.example.com/dir1/state/city
http://www.example.com/dir1/state/city/

Will also redirect correctly. Now, using this code, they're going to


http://www.example.com/template/C123/state/city

I've tried a variety of constructions, but none are working correctly. Any ideas?

Thanks!

[edited by: jdMorgan at 12:06 am (utc) on Jan. 24, 2008]
[edit reason] example.com [/edit]

jdMorgan

8:52 pm on Jan 23, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The code you show is an internal rewrite, not an external redirect. Its function is to map requested 'friendly' URLs back to the form actually used inside your server.

If external redirects were to be used, you would want to 301-redirect from the unfriendly format to the friendly format -- in order to remove previously-indexed unfriendly URLs from the search engines.

See the fourth post --and the recommendation-- in this previous thread [webmasterworld.com].

Jim

evarose

9:13 pm on Jan 23, 2008 (gmt 0)

10+ Year Member



That (internal rewrite) is exactly what I'm trying to do. My CMS uses the
http://www.example.com/dir1/template/C123/term structure; I've built the site using the
http://www.example.com/dir1/state/city/term structure for my URLs.

Most of the rewrites I have use this structure:


RewriteRule ^state\/city/?$ metro/C123

and work just fine. But when I tried to account for any additional strings on the end of the URL, I started getting these repeated segments.

Thanks!

[edited by: jdMorgan at 12:07 am (utc) on Jan. 24, 2008]
[edit reason] example.com [/edit]

jdMorgan

12:05 am on Jan 24, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Use separate rules, ordered from most-specific to least (generally, longest pattern to shortest) to handle all the possible requested-URL formats.

To avoid the Apache bug described in the document linked-to by the above-cited thread, you're generally better off to do all rewriting all-at-once in a single rule to any requested URL, rather than relying on a 'stack' of sequential rules to change bits and pieces at a time. So use the [L] flag on all of your rules, and make sure that neither the input URL to a rule, nor the output URL-path or file-path from that rule matches the pattern of any other rule (unless the first rule is an external redirect).

Some RewriteRule patterns, such as ".*" are pretty much guaranteed to trigger the apache bug. Others are less likely to do so, but I've never gone through the source code to analyze this.

Here's a thread with a solution to the Apache mod_rewrite bug [webmasterworld.com]. But I don't even use that code because it's complex and difficult to maintain; I wrote that code mostly as a proof-of-concept.

Jim

evarose

5:39 pm on Jan 24, 2008 (gmt 0)

10+ Year Member



Thanks so much for all the helpful information! I followed some threads down and did some searching, and was able to come up with the following working syntax:

RewriteRule ^(dir1¦dir2¦dir3)/state\/city/?([a-zA-Z_-]*)$ $1/template/C123/$2 [L]

jdMorgan

2:01 am on Jan 25, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No need to escape "/" slashes in your patterns; The regular-expressions syntax in mod_rewrite is not quite the same as in PERL or PHP.

Jim

evarose

3:39 am on Jan 25, 2008 (gmt 0)

10+ Year Member



Thanks! So the correct syntax would then be:

RewriteRule ^(dir1¦dir2¦dir3)/state/city/?([a-zA-Z_-]*)$ $1/template/C123/$2 [L]