Forum Moderators: phranque

Message Too Old, No Replies

Aliasing common file in each subdirectory to a central file

         

JohnKelly

5:39 pm on Oct 13, 2005 (gmt 0)

10+ Year Member



I'm setting up XML feeds for each subdirectory on my site. The XML button will link to [mydomain.com...] -- however I actually want it to run [mydomain.com...] which contains a PHP script that will output valid XML code based on the subdirectory name.

Since there are thousands of subdirectories, I don't want to create a separate XML file or .htaccess file for each one.

How can I use the root web .htaccess to have every .xml file for each subdirectory point to [mydomain.com...] without using a redirect (I want search engine spiders to still "think" they're seeing [mydomain.com...]

I'm thinking of the "Alias" directive in .htaccess but not quite sure if this is what I need, or how to set it up.

Thanks

JohnKelly

7:26 pm on Oct 13, 2005 (gmt 0)

10+ Year Member



After much fiddling around, here's what I came up with. I put the following in my web root .htaccess file.

RewriteRule ^DIR([^.]+)rss.xml$ /rss_master.xml?rsspath=$1

All the subdirectories are under a common "DIR" directory above, so the regex works for me. Some slight adjustments may be needed to fit someone else's situation.

jdMorgan

7:36 pm on Oct 13, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If your rsspath is bounded by "DIR/" and "/rss.xml" and if you really want to include the leading and trailing slashes in your variable, then the following tweaked pattern would be more efficient:

RewriteRule ^DIR(/[^/]+/)rss\.xml$ /rss_master.xml?rsspath=$1 [L]

If you do not wish to retain the slashes for use in the variable, then I'd recommend:

RewriteRule ^DIR/([^/]+)/rss\.xml$ /rss_master.xml?rsspath=$1 [L]

Either way, your use of the pattern "[^.]+" is questionable, since that pattern means, "match one or more characters not equal to a period." The modified pattern "[^/]+" means, "match one or more characters not equal to a slash." Thus, it stops attempting to match as soon as the second slash is encountered, avoiding several "back up and re-try" passes when trying to match the remainder of the pattern (the "/rss.xml" part), and thus speeds processing. Escaping the literal period can help eliminate unexpected results, and adding the [L] flag terminates rewrite processing for this pass *if the pattern matches and the rule is invoked.*

Jim

JohnKelly

7:44 pm on Oct 13, 2005 (gmt 0)

10+ Year Member



I will try that, thanks!