Forum Moderators: phranque

Message Too Old, No Replies

Is this the correct way to merge these rules?

         

JAB Creations

9:45 pm on Jan 17, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have a few directory rewrite rules that look like this...

RewriteRule ^[^/]*/admin(.+) admin$1 [QSA]
RewriteRule ^[^/]*/blog(.+) blog$1 [QSA]
RewriteRule ^[^/]*/contact(.+) contact$1 [QSA]


The setup is simple, with multi-domain support (one instance of the software, a database per domain) each site has it's own directory inside the same directory that contains the software...

admin/
blog/
contact/
site1.com/
site1.com/blog/
site2.com/
site2.com/contact/

This seems to work...

RewriteRule ^[^/]*/(admin|blog|contact)(.+) $1$2 [QSA]


I think I finally got the parenthesis/$1 bit figured out; (admin|blog|contact) is represented by $1 and (.+) is represented by $2 (and any other parenthesis in the order of their appearance).

However I'd appreciate input from others if this looks sound for my setup and if any improvements could be made please?

- John

g1smd

9:54 pm on Jan 17, 2012 (gmt 0)

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



QSA is the default action, so doesn't need to be mentioned.

Add the [L] flag to every rule.

I don't see how your rules don't simply end up with an infinite rewrite loop.

What is the [^/]* part for? What matches this part of the pattern?

JAB Creations

10:16 pm on Jan 17, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



example1.com/blog/
example2.com/blog/
example3.com/blog/
...all rewrite to...
blog/

...as are all other modules (admin, contact, forums, etc) hence multi-domain support which will launch before the end of this month with two websites using the software simultaneously.

The [^/] rule is to take the example1.com/ directory in to account so we can rewrite after.

I agree about QSA as removing it seems to have no effect.

- John

g1smd

10:25 pm on Jan 17, 2012 (gmt 0)

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



The RewriteRule RegEx pattern cannot match against the requested domain name or any query string.

JAB Creations

12:57 am on Jan 18, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm not talking about domain names or query strings.

For multi-domain support to work all these URLS...
root/user/example1.com/blog/
root/user/example2.com/blog/
root/user/example3.com/blog/
root/user/example4.com/blog/
...rewrite to...
root/user/blog/

I update a single instance of the software and everyone is updated. This is the exact same setup as having a million pages share the same XHTML and you update the background-color of the body element and then all million pages automatically have the new background-color. I'm not sure how else to explain it except with the word dynamic.

- John

lucy24

4:34 am on Jan 18, 2012 (gmt 0)

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



I'm not talking about domain names or query strings.

Your rule as originally written include the line [QSA], and a subsequent post said
example1.com/blog/
example2.com/blog/
example3.com/blog/
...all rewrite to...
blog/
<snip>
The [^/] rule is to take the example1.com/ directory in to account so we can rewrite after.

Seems as if you're not talking about anything but domains and queries-- neither of which is affected by a conditionless RewriteRule-- so the question remains: what is your rule intended to do?

g1smd

7:48 am on Jan 18, 2012 (gmt 0)

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



For this rule:
RewriteRule ^[^/]*/(admin|blog|contact)

and for this URL request:
http://example.com/blog/

only this is presented to the RegEx pattern:
blog/


The "left side" of the rule is the RegEx pattern matching the incoming URL request. It matches only requested path information.

The
[^/]*/
part appears to be redundant.

It does also have the unintented consequence of promoting duplicate content. All of these URL requests would match the pattern and be rewritten:
http://example.com/foobar/blog/

http://example.com/quux/foo/bar/blog/

http://example.com/unreliable/junk/blog/

to serve identical content.

phranque

1:38 pm on Jan 18, 2012 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



the RegEx allows exactly one forward slash immediately prior to (admin|blog|contact), so only the first example would match the pattern and be rewritten.
(and thereupon avoiding the rewrite loop)

[edited by: phranque at 1:41 pm (utc) on Jan 18, 2012]

g1smd

4:26 pm on Jan 18, 2012 (gmt 0)

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



Thanks for the correction. Poor choice of examples on my part, but yes, the comments about duplicate content still apply.