How to htaccess a file to be displayed from other domain? - .htaccess

I have a domain a.com with a file called style.css. And i have cookieless domain b.com. Both are on the same server. Now what i would like to acomplish is to write a htaccess rule that states:
if user wants to take b.com/style.css display him the file from the a.com/style.css folder.
I want to acomplish it with htaccess because i don't want to involve PHP to do this (performance issue) and i would like it to work transparent for the browser. What i mean the browser asks for file from b domain and gets it from b domain.
How to acomplish it with htaccess and does it impact performance?

You can try this rule:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?b\.com$ [NC]
RewriteRule \.(?:jpe?g|gif|bmp|png|tiff|css|js)$ http://a.com%{REQUEST_URI} [R=301,L,NC]
This does change change the URL to a.com/style.css.
If you don't want external redirection then only way out is to mod_proxy which may not be enabled already on your host. If you enable mod_proxy on b.com then you can try:
RewriteCond %{HTTP_HOST} ^(www\.)?b\.com$ [NC]
RewriteRule \.(?:jpe?g|gif|bmp|png|tiff|css|js)$ http://a.com%{REQUEST_URI} [P,L,NC]

Related

htaccess redirect to a directory

I'm facing a htaccess poroblem which I can't figure it out by myself.
Let's say I want to host two websites under the same hosting plan. The main website is a Prestashop eCommerce website. The other is a placeholder for another domain name.
For example, there is the main www.myshopdomain.com in the root and www.myutilitydomain.com in a directory called Utility.
Currently, all traffic to www.myutilitydomain.com is redirected to www.myshopdomain.com. What should I do to redirect all traffic for www.myutilitydomain.com to the Utility directory, preferably without any reference to www.myshopdomain.com in the redirected URL? Is it even possible? I know I can create a subdomain, but I need the myutility domain to be accessible directly.
I tried a few approaches and managed not to break the eCommerce site, but for www.myutilitydomain.com I always get Internal Server Error.
My final attempt was this:
RewriteCond %{HTTP_HOST} ^www.myutilitydomain.com
RewriteCond %{REQUEST_URI} !^www.myutilitydomain.com$
RewriteRule ^(.*)$ /Utility/
Your RewriteCondition is not right.
You can only match against URL path in a %{REQUEST_URI} RewriteCond not the host header.
Change it to
RewriteCond %{REQUEST_URI} !^/Utility [NC]

Need help in URL rewriting sub-domains

I recently registered a domain name kbcsurveyors.com. Then, I created two sub-domains, which created two new folders in the root folder.
My motive is that if I type kbcsurveyors.com/preinspection, it should point to preinspection.kbcsurveyors.com. Same for other sub domains.
In my .htaccess, which I placed inside root of mydomainname.com, I have written following lines:
RewriteEngine On
RewriteBase /
RewriteRule ^preinspection/(.*)$   https://kbcsurveyors.com/$1 [L,NC,QSA]   # Handle requests for "Preinspection"
But this file structure does not work. How do I write the .htaccess file to achieve what I want?
Regards
EDIT
I have asked a fresh question as this one has been messed up. Here is the link:How to rewrite rules for sub-domains
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^sub-domain1.mydomainname.com
RewriteRule ^(.*)$ http://mydomainname.com/$1 [L,NC,QSA]
If you want to redirect requests for a subdirectory to the appropriate domain, you can use the rules you have (or similar), but need to specify the scheme http:// and may use the R|redirect flag
RewriteRule ^sub-domain1 http://sub-domain1.mydomainname.com [R,NC,L]
If you also want to forward the requested path, you must capture it and use in the target
RewriteRule ^sub-domain1/(.*)$ http://sub-domain1.mydomainname.com/$1 [R,NC,L]

Redirecting to specific websites using error 301

I have two domain names bought mainly for SEO, I want to redirect them to certain pages on website of a different domain name.
I know I need to redirect them with 301's in the .htaccess.
But I'm getting confused, do I need to create essentially 3 .htaccess files one on each domain?
Thanks
No, you only need one .htaccess file per domain. However, be wary of including or excluding the www. in your URL, as you may render your SEO useless.
Redirecting to a non www. URL:
This assumes you are using Apache or something similar.
Open your existing .htaccess file using a text editor, and append the following code to it:
Options +FollowSymLinks
RewriteEngine on RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^example\.com
RewriteRule (.*) http://example.com/$1
[R=301,L]
Where example.com/ is your domain. As far as I am aware, you can append any page to this URL with the same effect.
Redirecting to a www. URL:
Again, assuming Apache or similar:
Open your .htaccess file in the text editor, and append:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1
[R=301,L]
Again, example.com/ refers to your own domain. Again, AFAIK you can append specific pages without issue.
With most websites, www.example.com and example.com will take you to the same place. If you haven't made any changes to this, I would advise using the former example.
Additionally, if you are using Wordpress, there are a vast number of different plugins that can handle this for you. For example (and I am not recommending this over any other plugin), https://wordpress.org/plugins/simple-301-redirects/
You may also hear the coderel="canonical" or see it from time-to-time. Some SEO professionals actually recommend this over 301, however with modern browsers and modern crawlers, this should no longer be an issue.

How can I redirect subdomain to folder while main domain points to another folder?

My very dear Stackoverflow community,
I have the following redirection problem and after several unsuccessful attempts I come here in search of enlightenment. My problem is the following. I have a domain, let's call it 'www.mydomain.com', and my 'public_html' directory has two folders as follows:
public_html
public_html/my_app/
public_html/my_other_app/
First, I would like that when typing the URL 'www.mydomain.com', I get redirected to the contents of folder 'my_app', while keeping the same URL. In fact this I have already accomplished, so whenever I type 'www.mydomain.com' I get redirected to 'www.mydomain.com/index.php', which actually corresponds to the 'public_html/myapp/index.php' script under 'myapp'.
Now I want to have a subdomain called 'other.mydomain.com', which has to redirect to contents of the 'my_other_app' folder, but I do not know how to make .htaccess work for this and at the same time work for the first case also.
So this is basically, the main domain redirects to one folder, and a subdomain redirects to another folder, and both folders are located under the public_html directory
Any hints more than welcome.
For your reference I post below my current .htaccess file:
RewriteEngine On
# redirect to www prefix
RewriteCond %{HTTP_HOST} ^mydomain\.com$ [NC]
RewriteRule ^(.*)$ https://www.mydomain.com/$1 [R=301,L]
# if start with www and no https then redirect
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www.mydomain\.com$ [NC]
RewriteRule ^(.*)$ https://www.mydomain.com/$1 [R=301,L]
# rewrite URL to trim folder
RewriteCond %{REQUEST_URI} !^/test/
RewriteRule ^$ /login [L,R=301]
RewriteRule ^(.*)$ test/$1 [L]
This actually works for my main domain, it also rewrites the url to https. I need to add something in here in order to process separately the 'other.mydomain.com' and redirect to the '/my_other_app/' subfolder
what you need is a vhost (virtual host) per app. In the vhost, you will define the vhosts root directory, which will point to either of your sub directories.
There is IP based vhosts (one IP address per subdomain) or name based vhosts (the vhost is chosen based on the HTTP host header that all modern browser send).
But there is too much to say about vhosts to write it all here, just read the apache documentation here:
http://httpd.apache.org/docs/2.2/vhosts/
I think with pure .htaccess files, you can't do that (I might be wrong). Normally you would add vhosts in the main apache config. Based on your hosting, this may not be possible. Talk to you hosting provider in that case.
Marc

How do I rewrite the url?

Could someone tell me how to rewrite this URL. I have looked at a lot of questions on stackoverflow but they seem to be missing my answer.
RewriteEngine On
That is what I have... its a bit poor.
I need to rewrite url's if they do not point to a directory.
I need to do this...
any.domain.com/pages/some-page-slug/login
To be rewritten to the correct url of...
any.domain.com/pages/login.php?page=32
Does anyone have any ideas on how this can be achieved?
1) Rewriting product.php?id=12 to product-12.html
It is a simple redirection in which .php extension is hidden from the browser’s address bar and dynamic url (containing “?” character) is converted into a static URL.
RewriteEngine on
RewriteRule ^product-([0-9]+)\.html$ product.php?id=$1
2) Rewriting product.php?id=12 to product/ipod-nano/12.html
SEO expert always suggest to display the main keyword in the URL. In the following URL rewriting technique you can display the name of the product in URL.
RewriteEngine on
RewriteRule ^product/([a-zA-Z0-9_-]+)/([0-9]+)\.html$ product.php?id=$2
3) Redirecting non www URL to www URL
If you type yahoo.com in browser it will be redirected to www.yahoo.com. If you want to do same with your website then put the following code to .htaccess file. What is benefit of this kind of redirection?? Please check the post about SEO friendly redirect (301) redirect in php and .htaccess.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^optimaxwebsolutions\.com$
RewriteRule (.*) http://www.optimaxwebsolutions.com/$1 [R=301,L]
4) Rewriting yoursite.com/user.php?username=xyz to yoursite.com/xyz
Have you checked zorpia.com.If you type http://zorpia.com/roshanbh233 in browser you can see my profile over there. If you want to do the same kind of redirection i.e http://yoursite.com/xyz to http://yoursite.com/user.php?username=xyz then you can add the following code to the .htaccess file.
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ user.php?username=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ user.php?username=$1
5) Redirecting the domain to a new subfolder of inside public_html.
Suppose the you’ve redeveloped your site and all the new development reside inside the “new” folder of inside root folder.Then the new development of the website can be accessed like “test.com/new”. Now moving these files to the root folder can be a hectic process so you can create the following code inside the .htaccess file and place it under the root folder of the website. In result, www.test.com point out to the files inside “new” folder.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^test\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.test\.com$
RewriteCond %{REQUEST_URI} !^/new/
RewriteRule (.*) /new/$1
TO do this you need to write a front controller.
See here, here, here, and here.
Alternatively in Apache you can rewrite this
any.domain.com/pages/32/login
or this:
any.domain.com/32/login
or even this:
any.domain.com/some-slug/32/login
to this:
any.domain.com/pages/login.php?page=32
One way or another to do this with only apache you need to supply the page id in some fashion. Keep in mind even with format any.domain.com/some-slug/32/login the content of the slug is irrelevant and won't necessarily link to the correct page. Which I imagine is undesirable and bad for SEO.
Another alternative is using RewriteMap. But this will be tricky and require reloading apache configurations whenever a page/slug is created/edit.
I understand that pages and login are static in this case and some-page-slug is changing. And you always want to redirect to static page /pages/login.php?page=32
So this is how to do it:
1) Rewrite
RewriteEngine On
RewriteRule ^pages/([a-zA-Z0-9_]+)/login(.*)$ /pages/login.php?page=32
or 2) Redirect Pernament
RewriteEngine On
RewriteRule ^pages/([a-zA-Z0-9_]+)/login(.*)$ /pages/login.php?page=32 [R=301,L]
or 3) Redirect Temporary
RewriteEngine On
RewriteRule ^pages/([a-zA-Z0-9_]+)/login(.*)$ /pages/login.php?page=32 [R=302,L]
Here is great article about htaccess trics
http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/

Resources