Forum Moderators: phranque

Message Too Old, No Replies

htaccess - two domains and not so good directory layout

         

gideonstorm

1:25 am on Apr 14, 2009 (gmt 0)

10+ Year Member



I did not plan to have multiple domains so I did not plan the directory structure well and I hope to fix someday but until then I have a problem I need help with:

I have two blogs
1. blog 1 is in directory /public_html
2. blog 2 is in directory /public_html/blog2

The code for the htaccess for blog 1 is and it works fine

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

The code for the htaccess for blog 2 is work in progress because my current code is not working. Here is some background.

The following directories are referenced when someone surfs blog2.com:
1. blog2.com/wp-content/themes/mytheme/press/
2. blog2.com/wp-content/themes/mytheme/tips/

when someone surfs the site in their browsers as blog2.com, the content is primarily pulled from /public_html/blog2.com/wp-content/themes/mytheme/press/

when they enter blog2.com/tips/index.html or error.html or thanks.html, the content should come from from /public_html/blog2.com/wp-content/themes/mytheme/tips/ but its not working (not found HTTP 302).

I am not sure how to write a htaccess to resolve this due to my lack of experience - I tried placing a htaccess file in the sub directory of blog2 with the following code

RewriteCond %{HTTP_HOST} blog2.com/tips/$ [NC]
RewriteCond %{REQUEST_URI} !^/public_html/blog2.com/.*$
RewriteRule ^(.*)$ blog2.com/tips/$1 [L]

Your help is appreciated.

jdMorgan

1:45 am on Apr 14, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



1) /tips is not part of a hostname, and so cannot appear in your first RewriteCond's pattern.

2) It is unlikely that /public_html is part of the URL, so it does not belong in your second RewriteCond's pattern.

3) ^.* and .*$ are unnecessary. You can delete either one without changing the pattern's behaviour in 99% of all cases.

4) Your rule says to get the content from /blog2.com/tips/xyz, not /blog2.com/wp-content/themes/mytheme/tips/xyz. Be sure you've got the right path in there.

Jim

gideonstorm

2:57 am on Apr 14, 2009 (gmt 0)

10+ Year Member



Thanks for your reply JdMorgan.

Is this what you are telling me

RewriteCond %{HTTP_HOST} blog2.com$ [NC]
RewriteCond %{REQUEST_URI} !^blog2.com/tips/.*$
RewriteRule /blog2.com/wp-content/themes/press/tips/$1 [L]

I have files error.html, thanks.html and index.html in /blog2.com/wp-content/themes/press/tips so when someone enter [blog2.com...] as an example, it should resolve and not state nothing found (404 or 302).

jdMorgan

2:24 pm on Apr 14, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Based on what you wrote (and didn't write) above, here's my best guess.

In /blog2/.htaccess:


# If "http://blog2.com/tips/xyz" request, rewrite to /wp-content/themes/mytheme/press/tips/xyz
RewriteCond %{HTTP_HOST} ^(www\.)?blog2\.com [NC]
RewriteRule ^tips/(.*)$ /wp-content/themes/mytheme/press/tips/$1 [L]
#
# Else rewrite all other http://blog2.com/xyz requests to /wp-content/themes/mytheme/press/xyz
RewriteCond %{HTTP_HOST} ^(www\.)?blog2\.com [NC]
RewriteCond $1 !^wp-content/themes/mytheme/press/
RewriteRule ^(.*)$ /wp-content/themes/mytheme/press/$1 [L]

If this does not work, examine the filepaths given in your server error log, and determine what is wrong with the filepath that the rules are rewriting to.

It is not clear how requests for the domain "blog2.com" are mapped to the server filepath /blog2. That is, I can't tell if this is already being done by code in httpd.conf or conf.d that was created by your "control panel" when you added the blog2 hostname (domain), or if this is already being done by code in httpd.conf or conf.d that was created during your blog2 installation. That is why I said this code is my "best guess" above.

All I can say is that the URL-paths and the filepaths in your code must be 100% correct, or the code won't work. Use your server error log to tweak the code to access the correct filepaths. You likely won't get this working without using the information in the error log.

Note that when working with .htaccess files, RewriteRule does not "see" the entire requested URL-path. Only the part of the path that follows the path to the current .htaccess file's directory is present in the URL-path that RewriteRule inspects. This is why I left out the "/blog2/" path-part in the RewriteRule patterns above.

On the other hand, the RewriteRule's substitution filepath must contain the complete server filepath to the directory or file that you want to rewrite to. But it's an open question whether the rewrite to the /blog2 filepath is already done in your server configuration or not: In this code, I have assumed that it has been already been done, and I start the substitution path with "/wp-content" instead of "/blog2/wp-content".

The bottom line is that the code is fairly simple. The hard part is identifying the correct URL-paths and filepaths that the code must use, ans this depends on the code's location and on the server configuration.

It is often helpful to "divide and conquer" the problem: Get the RewriteRules to recognize the requested URLs that need to be rewritten first, even if the target filepath ends up wrong. Concentrate on finding the correct RewriteRule patterns and conditions first, testing that all requested URLs that should be rewritten do get rewritten, and that all requested URLs that should not be rewritten don't get rewritten.

Only after that is working should you worry about getting the destination filepaths correct, so that you are only working on "one variable at a time." I often change the rewrites to redirects temporarily, rewriting one set of URLs to google.com and the other set of URLs to some other site. Once I have got the RewriteRule patterns correct, I change the substitution URLs back to my own server, and work on getting the correct filepaths. And once that is working, I change the rule syntax back from a redirect to a rewrite.

Jim

gideonstorm

6:53 pm on Apr 14, 2009 (gmt 0)

10+ Year Member



Jim, I really dont know what else to say about this problem. I will work with your code and see if it works for me. My initial testing of your code did not work but I am trying to get access to the logs so I can see more details. Thanks for providing a start

All pages, images and subdir under blog2.com/wp-content/themes/mytheme/press/ are rendered correctly with a browser as blog2.com (example blog2.com/staticpage1)

When I try to access pages under blog2.com/wp-content/themes/mytheme/tips/ as blog2.com/tips/index.html, it says nothing is found there. Perhaps this code is causing the issue since its part of the htaccess in the blog2.com directory also

Options All -Indexes
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

when I remove it things get worse. I am just not understanding this mod_rewrite stuff.

jdMorgan

7:19 pm on Apr 14, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In simplified terms...

In this example, the server takes the requested URL "blog2.com/tips/index.html" and effectively strips the "/blog2.com" off, leaving "/tips/index.html" as the requested URL-path.

The the server adds the DOCUMENT_ROOT declared in the virtual host's configuration (in one or more privileged server config files), converting the requested URL-path to a filepath. It then looks along that path for .htaccess files.

So, your server will be looking in blog2.com'z document root directory "/blog2.com/" for a file at "/bloq2.com/.htaccess".

The server will invoke mod_rewrite, which will parse that .htaccess file and find the rule that matches URL-paths starting with tips/<anything>. Mod_rewrite will then change the request to use the substitution filepath given in that rule, prepending the DOCUMENT_ROOT path to it. So the new path is now DOCUMENT_ROOT-plus-"/wp-content/themes/mytheme/press/tips/index.html".

The server will then re-start mod_rewrite processing, and if no further rules match, it will invoke the content-handler, and send the contents of the file DOCUMENT_ROOT-plus-"/wp-content/themes/mytheme/press/tips/index.html" back to the client. Note that if index.html is actually a script instead of a static file, then that script will be executed, and the contents that it produces will be sent back to the requesting client.

When the existing !-f !-d code is added to the consideration, behaviour depends on rule order. If this code precedes your new rules, then the new rules probably won't work, because this code rewrites any requested URL-path that does not resolve to an existing file or directory to the /blog2.com/index.php file path.

Jim

gideonstorm

9:38 pm on Apr 14, 2009 (gmt 0)

10+ Year Member



Jim, I must have said thank you so many times now. You are right that first set of code was causing the problem. I also found another .htaccess file in root of press that I deleted. I guess that was from prior testing I was doing with my prior code after trying to figure out mod_rewrite. You are the greatest for dealing with me - a novice and a newbie. the code below worked as is. Do you think I need the second part if this works as is?

Options All -Indexes
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On

RewriteCond %{HTTP_HOST} ^(www\.)?blog2.com\.com [NC]
RewriteRule ^tips/(.*)$ /wp-content/themes/press/tips/$1 [L]

RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

Andre

jdMorgan

2:02 am on Apr 15, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> Do you think I need the second part if this works as is?

I'm not sure what 'second part' you are referring to. Can you re-word that question more specifically?

In case we're in different time zones, I will say that if you test your site and *everything* is working, then of course you do not need to add more code.

Jim

gideonstorm

2:14 am on Apr 15, 2009 (gmt 0)

10+ Year Member



sorry about that. I meant this code:

# Else rewrite all other [blog2.com...] requests to /wp-content/themes/mytheme/press/xyz
RewriteCond %{HTTP_HOST} ^(www\.)?blog2\.com [NC]
RewriteCond $1 !^wp-content/themes/mytheme/press/
RewriteRule ^(.*)$ /wp-content/themes/mytheme/press/$1 [L]

I did not see a need for it since everything works fine with first condition you provided