mod rewrite exploding %{HTTP_HOST} - wordpress

I have a situation I've not come across before that calls for some interesting mod_rewrite rules and I cant find any examples of someone trying to achieve the same thing in a similar configuration.
Currently I have two domain names which are configured to share the same document root, in said document root is a dynamic php application which, based on the incoming hostname, displays content specific to the that domain.
The domains for example purposes are:
www.example1.com
and
www.example2.co.uk
(one being a TLD the other not)
In addition to this application there are two wordpress installations one for each of the two domain names. As we are not using wordpress MU here I need some fancy rewrites to firstly hide the wordrpess folder, and secondly present the request to the correct folders based on the HTTP_HOST.
Currently I have the following:
RewriteRule ^wp-content(.*) wordpress/example1$1 [L]
RewriteRule ^wp-admin(.*) wordpress/example1/wp-admin$1 [L]
RewriteRule ^wp-login.php$ wordpress/example1/wp-login.php [L,R=301]
And similar rules for content specific pages.
This works well for the single wordpress installation, but obviously not for the second, what I was hoping to do here was something like the following:
RewriteRule ^wp-admin(.*) wordpress/${HTTP_HOST}/wp-admin$1 [L]
However I need to remove the www. and .com from the ${HTTP_HOST} variable (or the www. and .co.uk )
Any suggestions on a way to achieve this or a better approach would be appreciated.

You can use RewriteCond to check for a pattern in HTTP_HOST and then capture part of that pattern.
For instance:
RewriteCond %{HTTP_HOST} ^(?:www\.)?([a-zA-Z0-9_-]+)\.(?:com|co\.uk)$
RewriteRule ^wp-admin(.*) wordpress/%1/wp-admin$1 [L]
The RewriteCond directive above checks to see whether HTTP_HOST fits a domain pattern ending ".com" or ".co.uk" and optionally beginning with "www.". If it does, it captures the interesting part of the domain name.
Then the RewriteRule (which only fires if the RewriteCond does match) is able to refer to the captured part of the RewriteCond pattern by using the %1 back-reference.
The pattern I've used in the RewriteCond above might not suit your needs perfectly, but once you know you can use a back-reference to a pattern captured by RewriteCond, it should be easy for you to use this to get the effect you need.

Related

Wordpress/Apache rewrite/redirect rule and regex

I need to do some apache rewrite/redirect rules to external webservice in case of 404 error for specific file extensions: .jpg, .png, etc. Wordpress is used here.
So, if 404 occurs at:
https://test.com/folder/subfolder/year/month/filename.jpg
I want to redirect it to:
https://test1.com/folder/subfolder/year/month/filename.jpg (external webservice, not the same phisical server)
I've tried such a configuration in htaccess, didn't work as expected:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*) test1.com/folder/subfolder//$year$\/$month$\/([^\s]+(\.(?i)(png | jpg | gif | svg))$)/
Do you have any ideas how to do it right way?
Any suggestions appreciated.
With your shown samples, attempts; please try following htaccess rules file. These rules are written as per shown domain names which are samples/tests, so you need to change values as per your actual values when you use them in your system. We also need to make sure that both (test.com and test1.com) are sharing same directory structure in your actual apache server.
Also make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{HTTP_HOST} ^(?:www\.)?test\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ https://test1.com/$1 [R=301,L]
To "redirect" URLs of the form /folder/subfolder/<year>/<month>/<file>.<png|jpg|gif|svg> where /folder/subfolder/ is static and the other elements are variable and which do not exist on the filesystem you would need to do something like the following before the WordPress code block. ie. before the # BEGIN WordPress section.
# Redirect certain non-existent image files to another server
RewriteRule ^folder/subfolder/\d{4}/\d\d/[\w-]\.(png|jpg|gif|svg)$ https://test1.com/$0 [R=302,L]
# BEGIN WordPress
:
The <year> is a 4-digit number and <month> is a 2-digit number. The filename can consist of the characters 0-9, a-z, A-Z, _ (underscore) and - (hyphen).
This should presumably be a 302 (temporary) redirect, not a 301 (permanent), otherwise if the resource should become available at the source domain then it won't be accessible to those users who have visited the URL before (they will be redirected from cache).
To avoid the external redirect it may be preferable to "proxy" the request to the other domain. (This is invisible to the end user.) Although this potentially involves additional configuration server-side, as you would need to configure the source server as a "reverse proxy". You can then replace the R=302 flag in the above rule with P (proxy).

Redirecting URL with paramter to top-level domain

I have been trying to search but no luck yet. Found different options about redirecting but nothing like what I'm looking for.
So currently the website URLs contain lang parameter like ?lang=en, ?lang=ru, ?lang=fi. Parameters are on the very end of the URL.
Idea is to move languages to top level domain. So basically I'm looking for a way to redirect all URLs that contain parameter ?lang=ru to top-level .ru domain. Same with other languages.
Can I do it via .htaccess or it shouldn't be done at all? Moving site to different domains should need redirection to pass the link juice and authority to new domains.
Hopefully, someone can lead me to the correct way of doing it.
Much appreciated!
If there are no other URL parameters that need to be preserved then you can do it like the following using mod_rewrite near the top of your .htaccess file:
RewriteEngine On
# Redirect "/foo?lang=xx" to "example.xx/foo"
RewriteCond %{QUERY_STRING} ^lang=([a-z]{2})$
RewriteRule ^ https://example.%1%{REQUEST_URI} [QSD,R=302,L]
The above matches any language code that consists of 2 lowercase letters. To match specific language codes then use alternation in the regex and change the RewriteCond directive to read:
RewriteCond %{QUERY_STRING} ^lang=(en|ru|fi|abc|etc)$
The QSD flag discards the original lang=xx query string from the redirect response (Apache 2.4). Otherwise this will be copied onto the target URL by default.
The %1 backreference contains the value of the lang URL parameter captured in the preceding condition. The REQUEST_URI server variable contains the full URL-path (no query string) from the request.
This does assume that the language specific TLD domains are hosted elsewhere. In other words, we do not need to check whether we are already at the required TLD domain.
Test with a 302 (temporary) redirect and only change to a 301 (permanent) redirect - if that is the intention - once you have confirmed that this works OK.
UPDATE: Any specific redirects, eg. lang=en to .com will need to appear first. For example:
# Redirect languages to .com
RewriteCond %{QUERY_STRING} ^lang=(en)$
RewriteRule ^ https://example.com%{REQUEST_URI} [QSD,R=302,L]
# All other language codes...
# Redirect "/foo?lang=xx" to "example.xx/foo"
RewriteCond %{QUERY_STRING} ^lang=([a-z]{2})$
RewriteRule ^ https://example.%1%{REQUEST_URI} [QSD,R=302,L]
Use alternation (as mentioned above) if there are more language codes. eg. (en|gb).
You should be able to do this via .hataccess. You can use the code below in your .htaccess
RewriteEngine on
RewriteCond %{REQUEST_URI} lang=en
RewriteRule ^ https//wwww.yourwebsite.ext [L,R]
I think it should work fine if you write same for all the others. DO give it a try and let me know.

HTACCESS - Change Directory Structure

I recently changed our URL structure for several different pages but I'm having some trouble with the HTACCESS rewrites. I've included the examples below and I'm hoping that someone can help me with the correct rule to use!
http://www.tintworld.com/ny/albany-022/home-window-tinting/ <-- Original URL
http://www.tintworld.com/albany-ny-022/home-window-tinting/ <-- New URL ('ny' is moved)
I'm currently trying to use the following rule but it's not redirecting the original URLs...
RewriteCond %{HTTP_HOST} ^www\.tintworld\.com$ [NC]
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^ny/albany-022/$ http://www.tintworld.com/albany-ny-022? [R=301,NE,NC,L]
Please help. :-)
The problem is likely due matching ^ny/albany-022/$ in your RewriteRule. The $ in regular expressions denotes the end of the line. Try removing the $, and adding the remaining part onto the rewritten URL
RewriteRule ^ny/albany-022/(.*) http://www.tintworld.com/albany-ny-022/$1
The $1 here puts in the part that's matched by the (.*).
if you have several different pages, I would recommend using a plugin like
http://www.weberz.com/plugins/404-redirected/
to keep track of 404 pages and redirect them

Wordpress Multisite (subdomain install) rewrite rules

1) I have a Wordpress multisite subdomain installation with about 100 sites in the network.
Network Admin - Sites (All Sites) shows them like this:
main site: "www.domain-name.com"
all other sites: "subdomain-name.domain-name.com"
there is also a domain mapping plugin, so they all get access as separate urls:
domain-name1.com
domain-name2.com
...
domain-name100.com
2) Currently urls on all blogs are set to:
domain-name.com/2012/10/26/post-name
domain-name.com/category/category-name
(I used today's date as an example)
This is also the way domains show in Google and Bing search results
3) I am going to remove "2012/10/26/" and "category/" from the urls, so they are gonna look like this:
domain-name.com/post-name
domain-name.com/category-name
...and I need to have a rewrite rule that takes care of redirecting people coming from serps to these new urls
4) I have something like this right now:
RewriteRule ^([0-9]{4})/([0-9]{2})/([0-9]{2})/(.*)$ http://domain.com/$4 [L,R=301]
RewriteCond %{HTTP_HOST} ^domain\.com
RewriteRule ^([0-9]{4})/([0-9]{2})/([0-9]{2})/(.*)$ http://domain.com/$4 [L,R=301]
but I am not sure if it is correct / if it is gonna work right, and also I need to have a rule for all 100 websites, and not just one. These versions (only one of them was meant to be used) dont include the "category/" part either.
5) If somebody would have an idea how to do that it would be great (one or two rewrite rules for all sites would be probably best).
Thank you in advance for any info.
You can simplify your regex for the RewriteRule using \d to match digits and only using a match group on the post-name. If this behavior is the same for all of the domains, you can just omit the domain from your RewriteCond and RewriteRule:
RewriteRule ^\d{4}/\d{2}/\d{2}/(.*)$ $1 [L,R=301]
RewriteRule ^category/(.*)$ $1 [L,R=301]
You can test the above rules and resulting URLs with http://htaccess.madewithlove.be/

Redirecting subdomains to wordpress content

I have one single Wordpress application on domain www.mywpsite.com.
I have static pages:
a. www.mywpsite.com/subsite
b. www.mywpsite.com/subsite/child1
c. www.mywpsite.com/subsite/child2
and need that content to be respectively addressed using a subdomain like this:
a. subsite.mywpsite.com
b. subsite.mywpsite.com/child1
c. subsite.mywpsite.com/child2
Note that there is no physical structure for the subdomain, it is just a way of aliasing, and there is only one Wordpress installation under one document root. Also, I have a few pages and just one subdomain to work with, so a 'case by case' solution is valid.
Eventually I will also use the same subdomain for some dynamic content:
d. www.mywpsite.com/category/subsitenews => subsite.mywpsite.com/news
Finally, if possible, I need that the url retained in the address bar is the one using the subdomain, I mean:
The user types subsite.mywpsite.com/some-static-or-dynamic-content.
Redirection is performed to the appropriate wordpress content, and content is delivered.
The url in the user is still the same using subdomain, instead of the redirected url.
How can I do this?
I think I must use .htaccess but I have no idea how it works. I'm not sure if other questions I have found have to do with my problem.
redirect subdomain and retain url structure
.htaccess redirect ~ [fake-subdomain.domain.com/*] to [domain.com/*]
Thank you very much.
If the subdomain's aren't on the same server and under the same document root as the main domain, then you'll need to use a reverse proxy, or the P flag using mod_rewrite. You can try adding these rules to the htaccess file in your document root, preferably above any rules that may already be there:
RewriteEngine
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?([^/]+)/([^/]+)/?$ http://$1.mywpsite.com/$2 [L,P]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?([^/]+)/?$ http://$1.mywpsite.com/ [L,P]
The problem here is that dynamic content, ones that wordpress dynamically generates, will also be sent to the subdomains. There is no way to tell on the htaccess level what's valid dynamic content and what isn't. In order to check for that, you'll need to do it from within the wordpress CMS, probably some way of doing that but it'll require custom code or at least a plugin and possible customizing the plugin.

Resources