Forum Moderators: phranque

Message Too Old, No Replies

Yet another mod-rewrite question

         

smbaker

6:49 pm on Mar 2, 2010 (gmt 0)

10+ Year Member



This started out as a question to the wordpress folks, but it really has more to do with mod_rewrite.

Let me start by describing the scenario. I have two domains, site1.com and site2.com. Site1.com is the primary domain of my site and points to the root of my html directory tree. Site2.com is a secondary domain that points to /site2/ inside of site1.

site1:
/.htaccess - apache htaccess file
/index.php - wordpress index for site1
/wordpress - the guts of the wordpress installation
/site2/ - files for site2.com
/site2/index.php - a wordpress index.php that includes /wordpress/wp-blog-header.php (i.e. site1's wordpress installation)


Now, what I want to do is to have some pages of site2 rewritten to pages in site1. For example, lets assume I want [site2.com...] to return page #360 on site1.

I've tried a number of things that should have worked but didn't. I finally settled on this, which shouldn't work but does:

site1:/.htaccess:
RewriteCond %{HTTP_HOST} ^www\.site2\.com
RewriteRule ^site2/$ /index.php?page_id=360 [L]

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>



The /index.php in my rewrite rule doesn't seem to mean anything. I can change it to anything I want. For example, this will work:


RewriteRule ^site2/$ /somepagenameimadeup?page_id=360 [L]


The page_id=, well that does matter. It's the ID of the page I want displayed.

What I don't get is that I'm using the [L] flag on the rewrite rules, which (if I understand correctly) is supposed to prevent further rewrite rules from being applied. By my reckoning, rewriting it to /somepagenameimadeup?page_id=360 should have resulted in a 404 error and we'd be done with it. But... it didn't. It managed to make it to an index.php (the index.php in /site2/index.php to be specific) and actually render page #360.

I'm also greatly confused about the last rule in the wordpress section. The one that rewrites '.' to index.php. If '.' matches any single character, then doesn't wouldn't this replace one character with index.php? For example, "http://site1.com/foobar" --> "http://site1.com/index.phpfoobar" ?

Thanks,
Scott

jdMorgan

8:15 pm on Mar 3, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The [L] flag stops processing for this iteration through the rules within this current HTTP request.

If you created "site2" as a "add-on domain" using your control panel, then if you want the code to apply to site2, it must be placed into /site2/.htaccess. Also in this case, requests to "site2" may not be able to access files in site1, although site1 can usually access site2's files with this kind of set-up.

In order to work this out, simplify things and take one step at a time. Eliminate internal dependencies by getting a simple redirect in site2 working first -- redirect all requests to google.com, for example. then change the rule's pattern to math the exact URL you want to redirect. then add a RewriteCond to examine the query string (if needed). Once that works, change the redirect target address to the URL on your site1. And if that works, then change the syntax from a redirect to an internal rewrite and test that.

"." matches a single character, but since that "." pattern is not anchored by "^" and "$" it can be followed by any number of additional characters. An un-anchored dot means "match any URL-path that contains at least one character." For more information, see the concise regular-expressions tutorial cited in our Apache Forum Charter. I suggest that you do not try to use mod_rewrite before becoming familiar with the basics of regular expressions -- doing so risks doing huge damage to your site's correct operation and search ranking, as one single typo in a rewriterule can have disastrous effects.

Also, see the serveral concurrent WP threads for a big speed up on WP's feeble attempt at .htaccess code -- *after* you get your basic rule working, that is...

Jim