.htaccess with partially matching domain name - .htaccess

I have a really basic set of rewrite conditions in my .htaccess file, which doesn't work for one condition.
I have run it through a .htaccess tester which claims it should work, but it doesn't on the real site.
I have site.com, site.com.au, anothersite.com.au which should all redirect to site.com.au
RewriteBase /
# Domain Aliases
Rewritecond %{HTTP_HOST} !^www\.site\.com\.au$ [NC]
RewriteRule (.*) http://www.site.com.au/$1 [L,R=301]
When I visit anothersite.com.au, it 301s successfully to site.com.au, however when I visit site.com it just stays at site.com and doesn't 301. I have used ^ and $ which should limit the match to the exact .com.au domain but it still seems to just decide that .com is enough and doesn't redirect.
I have also tried using individual rules for all of the domains- this behaves in the same way.
Is anyone able to shed some light on this for me?

Related

Redirect all but some specific traffic via .htaccess..?

I have a file at example.com/DesktopModules/SubscriptionSignup/Tools/IPNHandler.aspx that needs to be rewritten so that it actually runs example.com/paypal-ipn-handler.php.
All other traffic, though, should be redirected to another-example.com.
I'm using this in my .htaccess file:
# Rewrite IPNHandler.aspx to paypal-ipn-handler.php
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_URI} ^/Tools/IPNHandler.aspx [NC]
RewriteRule ^.*$ https://www.example.com/paypal-ipn-handler.php [P]
</IfModule>
#Redirect all other traffic to new domain.
RewriteRule ^ https://www.another-example.com%{REQUEST_URI} [L,R=301]
However, that's redirected everything including the URL that should stay at this domain, but get re-written to the PHP file.
For example, with the above in place, I would expect that traffic to example.com/DesktopModules/SubscriptionSignup/Tools/IPNHandler.aspx would remain at example.com, but run the PHP script instead. This is not happening, though. It's getting redirected to another-example.com/..../IPNHandler.aspx and gives me a 404, of course.
Any information about how I can adjust this so that my rewrite works and stays on the original domain, but all other traffic gets redirected would be greatly appreciated. Thanks!
EDIT
Actually, I commented out the redirect to see if my rewrite was working, and it's actually giving me a 404, but when I hit the paypal-ipn-handler.php directly I get the output I expect.
So it seems I need more help than I thought, please, and thanks!
You may use these rules in your site soot .htaccess:
RewriteEngine On
RewriteRule Tools/IPNHandler\.aspx$ /paypal-ipn-handler.php [L,NC]
#Redirect all other traffic to new domain.
RewriteRule !^paypal-ipn-handler\.php$ https://www.another-example.com%{REQUEST_URI} [L,NC,NE,R=301]
There is no need to use P flag here as you just want an internal rewrite.
Condition !^paypal-ipn-handler\.php$ will redirect everything except /paypal-ipn-handler.php.
Make sure to use a new browser to test or test after you completely clear browser cache.

mod_rewrite - change top level domain

im trying to find out an issue with the following mod_rewrite Rule:
On my Account on "Strato" (Hoster) the Rule is working, however it is not working on "1und1" (another Hoster). Is there anything wrong with that rule?
# Rewrite Rule
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^.*original-domain.de [NC]
RewriteRule /* http://new-subdomain.anotherdomain.de%{REQUEST_URI} [P]
# End Rewrite Rule
Is there perhaps another or even better way to get the redirect done?
The Goal is that a User who is visiting http://www.original-domain.de (with or without www.) should see the content of http://new-subdomain.anotherdomain.de but the original-domain Adress still stays in the Adressbar of the Browser. In other words, the User won't even recognize that he got redirected.
And, another Question: What stands the [P] for after the Rewrite Rule? If i change it to [L] the redirect works but with the new-subdomain.anotherdomain.de Adress beeing displayed in the Browser? Couldn't find a Website that describes that properly.
Thanks for your help in advance!

htaccess Rewriterule for Multilanguage Website

I have a Multilanguage-Website with 2 domains like www.domain.com and www.domain.de
Now I want to define some RewriteRules like:
RewriteRule ^category/(.*)$ http://www.domain.de/$1 [R=301,L]
But I dont know how I can change it to work for both domains at once. Changing that target just without the domain doesnt work.
Example:
RewriteRule ^category-[0-9]{1,4}/(.*) http://www.domain.de/category/$1 [R=301, L]
This would redirect the user to the .de-domain. Doesnt matter which domain he was on preview pages. So visitors from domain.com would be suddenly on domain.de on some specific pages with this rule.
I want this rule to work for both domains and dont know how to write the target path.
I found a solution.
RewriteBase /
and RewriteRule without domain

URL Rewrite seems to be functioning like a URL Redirect

This is the rewrite section of my .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^komarketingassociates\.com$ [NC]
RewriteRule ^(.*)$ http://www.komarketingassociates.com/$1 [L,R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.php\ HTTP/
RewriteRule ^(.*)index\.php$ /$1 [R=301,L]
RewriteRule ^industry-news/.*[$]([0-9]+)\.htm$ http://www.komarketingassociates.com/industry-news/article.php?articleid=$1 [L]
The last rewrite rule is meant to process requests for our repository of news articles. Until maybe a week ago it was working perfectly translating the SEO friendly link titles like this one, companies-are-becoming-more-accustomed-to-leveraging-social-media$715.htm into the proper format to retrieve them from the database. Recently, however, without any changes being made to the .htaccess file (to my knowledge) the links now seem to use this redirect to article.php?articleid=715 rather than doing it server side. The links on the site are still the seo friendly urls, but now it seems as if you are being redirected to the article template page.
We are using GoDaddy to host our site. PHP version 5. I am completely stumped as to how this is happening and how to fix it. Any help is appreciated!
Thanks!
-Bill
Proxy flag. (P)
Warning, will mess with IPs. Every single IP will look like it's your servers. However, mod_rewrite sets the X-FORWARDED-FOR when you use P, so you can extract (I'd recommend validating the IP as being real as well before trying to use it in any SQL) and use the IP address still.

htaccess redirect for subdomains -> similarly-named subdirectories?

I'm restructuring a web site with a great deal of content currently parked at URLs that look like this.
http://string.domain.com/year/month/dd/string-pulled-from-title
For various reasons, I'd like to park all new content at URLs that looks like this
http://www.domain.com/blogs/string/year/month/dd/string-pulled-from-title
I'd like to make the change for future content, but don't want all the old stuff to go 404.
I believe a 301 redirect rule in my htaccess will do the trick, sending all referred traffic coming in through old links to the new formats.
But what should this rule look like? I've read a few tutorials but haven't found this exact case in any examples.
Note, I don't want to do this for all subdomains, only for about 10 specific ones. So if someone could help me figure out one of these, then I can copy paste it 10 times in my htaccess for each subdomain and be set.
Drop this into the .htaccess file of the old site (adjusting the domain to your actual one):
RewriteEngine On
RewriteRule ^(.*)$ http://example.com/blogs/string/$1 [R=301]
This will grab this part of the URL at the old site:
year/month/dd/string-pulled-from-title
and redirect it to the new site under the new location:
blogs/string/year/month/dd/string-pulled-from-title
Alternatively, if you want something a little more variable like, without having to custom fix each .htaccess, drop this in the file for each subdomain instead:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*).example.com
RewriteRule ^(.*)$ http://example.com/blogs/%1/$1 [R=301,L]
If you're redirecting to the same domain, and it includes the www, adjust the rewrite rules to the following:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*).example.com
RewriteCond %{HTTP_HOST} !^www.example.com [NC]
RewriteRule ^(.*)$ http://example.com/blogs/%1/$1 [R=301,L]
Note the second RewriteCond which checks to make sure that the URL requested does not include the leading www, which may lead to an endless redirect if the destination URL itself includes www and would try and redirect that subdomain as well.
%1 grabs the first capture group from the line above.
$1 references the first capture group on the same line.

Resources