URL rewriting .htaccess help needed - .htaccess

Assume I have two domain names example1.com and example2.com. I forwarded example1.com to example2.com using the domain forwarding on hosting panel. It works fine but when I go to example1.com/sub it just shows example2.com as the URL. I want it to show example2.com/sub. I tried URL rewriting but no luck so far. It just keep loading and shows nothing. Am I missing something ?
This the rule I used.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^example1.com$ [NC]
RewriteRule (.*) http://www.example2.com/$1 [R=301]

First make sure that you've cleared your browser's cache. If it's a 301 redirect your browser will cache the redirect. Then make sure to turn off your hosting panel's forwarding.
Other than that, your rule should work fine, assuming it's at the top of the htaccess file in example1.com's document root.

In RewriteCond rule you use !, which mean negate the result of the condition.

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.

URLs redirection and masking via .htaccess (only subpages without root domain)

I've tried many things to solve this and none of them worked the way I need and it's driving me crazy...
I have domainA.com and domainB.com. On domainA.com I have a frontend (just one page website) and on domainB.com I have a backend. domainB.com is an IP address. So, I want to show the backend with pretty URLs and I want to use domainA.com. So, if someone goes to domainA.com/randompagename it should show the content from domainB.com/randompagename, but the URL should stay domainA.com/randompagename.
I've been able to accomplish this with:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domainA.com
RewriteRule ^(.*) http://domainB.com/$1 [P]
But it also redirects the root domain and domainA.com shows the content from domainB.com which I don't want to happen.
I want domainA.com to show the content from domainA.com and domainA.com/anypage to show content from domainB.com/anypage
Is this possible? Any help would be greatly appreciated!
Try your rules this way and see how that works out.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domainA.com
RewriteRule ^([^/]+)/?$ http://domainB.com/$1 [P]

How to show content on one domian from another domain without changing URL through htaccess

Let say I have two domains, domainA and domainB. Whenever somebody hits domainA in browser the contents from domainB should load. Is this possible?
I have tried below rules, but it redirects to domainB.
RewriteCond %{HTTP_HOST} ^domainA.com
RewriteRule ^(.*) http://domainB.com/$1 [P]
Thanks.
Your rewrites look ok. Make sure mod_proxy is installed on the server.
Also, your RewriteCond pattern wouldn't match www.domainA.com, so you might want to drop the carent.

Redirect domains and subdomains with .htaccess

Right now *.foo.com and *.bar.com are aliased to the same vhost with LAMP. Right now it, of course, shows the same content with every URL. But I don't actually want this to be what happens, and I want the other domains to actually redirect to foo.com. I'm assuming this is possible with the .htaccess file.
EDIT: I'm hosting this on my own server, so I'd also like the IP address to redirect to foo.com
Use a rewrite condition in your .htaccess to selected everything not for foo.com and redirect it then:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^foo\.com$
RewriteRule ^(.*)$ http://foo.com/$1 [R=301,L]

Redirect But Not Show The URL redirec :: htaccess?

I need to redirect some users, possibly to a subdomain, but I do not want them to know that they are in a subdomain. They should think they are in the main domain.
I believe this can be done with htaccess, but it is gibberish to me.
Can someone please throw me some bones?
When serving content from a different domain without redirecting the browser (thus changing the URL in the address bar) one of two things needs to happen. Either there is a file-path resolution to the other domain, or a reverse proxy must be set up (and it's pretty easy to do if mod_proxy is loaded).
It looks like you have your subdomain inside the document root of your main domain, which means this option will be viable. So if you want to it so when someone puts this URL in their address bar, http://domain.com/page, they get served the content in http://sub.domain.com/another-page, you'd simply add these rules to the top of the htaccess file in the document root of your main domain (public_html):
RewriteEngine On
RewriteRule ^page(.*)$ /subdomain_folder/another-page$1 [L]
Otherwise, the second option is to use mod_proxy:
RewriteEngine On
RewriteRule ^page(.*)$ http://sub.domain.com/another-page$1 [L,P]
EDIT:
I want to redirect some users, depending on their ip (geographic location) to another subdomain. And yes public_html is the root
You can check against the IP via the %{REMOTE_ADDR} variable:
RewriteCond %{REMOTE_ADDR} ^123\.45\.67\.89$
RewriteCond !^/subdomain_folder
RewriteRule ^(.*)$ /subdomain_folder/$1 [L]

Resources