mod_rewrite - change top level domain - .htaccess

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!

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.

How to redirect a new page (wrong URL) to another page?

I'm new at programming. We have an office project, the website's URL is www.project.com.ph (sample name), this is already a live website from the client. But the released printouts have the instructions for the users to go to www.project.com/ph which is wrong and we can't reprint the material since it already reached plenty of event places.
Now the problem is, we need to redirect to www.project.com.ph automatically if the users type in the browser's address bar www.project.com/ph. I ask if this is possible without any kind of CMS or Wordpress and how to actually do it? We bought a new domain www.project.com for this. Any kind of help is appreciated.
Try the following near the top of your .htaccess file in the root of www.project.com. This works OK (although marginally less efficient) if both domains are pointing to the same place, since it specifically checks that we are requesting the "wrong" domain:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?project\.com$ [NC]
RewriteRule ^ph/?(.*) http://www.project.com.ph/$1 [NC,R=302,L]
This will redirect requests for www.project.com/ph (no slash), www.project.com/ph/ (with a trailing slash) and www.project.com/ph/<whatever> to http://www.project.com.ph/<whatever>.
This is a temporary (302) redirect. Change it to a permanent (301) only when you are sure it's working OK.
From kj.'s answer on a similar question, here
In your .htaccess for www.project.com, this should do the trick.
RewriteEngine on
RewriteRule ^(.*)$ http://www.project.com.ph/ [R=permanent,NC,L]
This will redirect any request to project.com to the domain http://www.project.com.ph/
To include the path after the /ph/` you can use this.
RewriteEngine on
# redirect including path after ph/ (e.g. project.com/ph/directory/file.php to project.com.ph/directory/file.php
RewriteRule ^ph/(.*)$ http://www.project.com.ph/$1 [R=permanent,NC,L]
# redirect any other requests to project.com.ph index
RewriteRule ^(.*)$ http://www.project.com.ph/ [R=permanent,NC,L]
You can redirect (301 redirect) the URL using RewritrRule in .htaccess file
RewriteRule "http://www.project.com/ph/(.*)" "http://www.project.com.ph/$1" [L,NC,R=301]

.htaccess rewrite conflicting rules

I'm having an issue with some htaccess rules which I thought would be simple. I have some nice SEO friendly URL rewriting in place as below
RewriteEngine On
RewriteCond %{REQUEST_URI} !^(index\.php|/images|/templates|/views|/ajax|/uploads|/robots\.txt|/sitemap\.xml|/favicon\.ico|/scripts|/cron|/combine.php|/js|/css)
RewriteRule ^(.*)$ index.php?ref=$1&%{QUERY_STRING} [L]
This all works well and I want to keep this. I also wish to rewrite some old pages which Google WMT is reporting as 404's to the new equivalent and for that I'd like to use:
Redirect 301 /about_us http://example.com/about-us
The problem I have is that the URL that the browser is directed to is:
http://example.com/about-us?ref=about_us
The about_us is the old link and about-us is the correct link. If the htaccess redirected to example.com/about-us then the other SEO friendly rewrite rule will pick it up and show the page but eh extra ?ref= parameter is confusing it. I am guessing the two rules are conflicting to a degree but is there a way to get the two rules to work together e.g. redirect without the extra ?ref= parameter? My knowledge of htaccess is basic to say the least so I am a little stuck on this one.
Thanks in advance
Redirect and RedirectMatch are part of mod_alias, while the rewrite rules are part of mod_rewrite. The problem you're running into is when you mix the two, both modules affect the same request, thus two things happen when you only want one. In this case, you need to stick with just mod_rewrite and use this instead:
RewriteEngine On
RewriteRule ^about_us /about-us [L,R=301]
RewriteCond %{REQUEST_URI} !^(index\.php|/images|/templates|/views|/ajax|/uploads|/robots\.txt|/sitemap\.xml|/favicon\.ico|/scripts|/cron|/combine.php|/js|/css)
RewriteRule ^(.*)$ index.php?ref=$1&%{QUERY_STRING} [L]
Note that the rule that redirects comes before the rule that routes to index.php.

Using mod_rewrite to mask a directory/file name in a URL

I've taken my site down for some prolonged maintenance and am using mod_rewrite to send all requests to a single page: www.mysite.com/temp/503.php
This is my .htaccess file which works fine.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/temp/503.php [NC]
RewriteRule .* /temp/503.php [R,L]
However, what I'd also like to be able to do is to hide /temp/503.php in the resulting URL from the visitor.
I know this is perhaps trivial and I'm sure fairly simple to achieve, but with my limited mod_rewrite skills I can't seem to get it to work.
Any help would be much appreciated.
Thanks.
Just get rid of the R flag in the rewrite rule, which tells the rule to redirect the request, thus changing the URL in the browser's location bar. So the rule would look like:
RewriteRule .* /temp/503.php [L]
which internally rewrites the requested URI instead of externally telling the browser that it's been moved to a new URL.

.htaccess with partially matching domain name

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?

Resources