Forum Moderators: phranque
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.
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
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).
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]
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
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.
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
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
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
# 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