subdomains and url rewrite - redirect

Good morning, i have a problem and i don't find a solution.
I need to point multiple domain to same folder but my hosting when i create a subdomain point to a folder with the same name of the subdomain.
The result that i want to have is:
demo.domainname.tld/index.php will point to www.domainname.tld/shop/index.php?username=demo
demo.domainname.tld/product.php?id=100 will point to www.domainname.tld/shop/product.php?id=100&username=demo
test.domainname.tld/index.php will point to www.domainname.tld/shop/index.php?username=test
test.domainname.tld/product.php?id=100 will point to www.domainname.tld/shop/product.php?id=100&username=test
keeping in the url the subdomain address.
I am creating a ecommerce platform and i want to use an unique ecommerce and load different content by keeping the username from subdomain.
it is possible? how?

A long as all the *.domainname.tld DNS entries point to the same IP as www.domainname.tld and they all share the same document root, you can add these rules to the htaccess in the document root:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.domainname\.tld$ [NC]
RewriteRule ^/?index\.php$ /shop/index.php?username=%1 [L,QSA]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.domainname\.tld$ [NC]
RewriteRule ^/?product.php$ /shop/product.php?username=%1 [L,QSA]
If you want to actually do a redirect, thus changing the URL in the browser's address bar, include a R flag in the square brackets so they look like: [L,QSA,R] or [L,QSA,R=301] for a permanent redirect.

Related

Redirect rule to allow sub.domain.com to only serve images

I need help with a redirect rule.
I created a subdomain sub.abc.com and set it's public_html to that of abc.com.
This means public_html is now accessible either by abc.com or by sub.abc.com .
I want to limit this so that sub.abc.com only provides access to images in wp-content/uploads any other requests to sub.abc.com should be redirected to abc.com/url/url .
Here is what i have tried creating
RewriteCond %{HTTP_HOST} ^cdn.example.com
RewriteCond %{REQUEST_URI} !^/wp-content/uploads/$1
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]
so if the user is accessing cdn.example.com/wp-content/uploads/1/1/file.jpg then there is no redirect, but if they are trying to access cdn.example.com/post/post or cdn.example.com/anything-else then it will redirect to example.com/post/post or example.com/anything-else .
The following rule should work for you. Remember to put it at the of your htaccess or server.config file before others rules otherwise these rules may override it.
RewriteEngine on
#serve only images on abc.example.com
#redirect to main domain if request is not for images
######################
#if the host is "sub.example.com"
RewriteCond %{HTTP_HOST} ^sub\.example\.com$ [NC]
#and the request is not for images and /wp-content/uploads
RewriteCond %{REQUEST_URI} !/wp-content/uploads/?$ [NC]
RewriteCond %{REQUEST_URI} !\.(jpg|png|gif|jpeg)$ [NC]
#redirect the request to main domain
RewriteRule ^ https://example.com%{REQUEST_URI} [L,R=302]
This will redirect your sub.example.com with request string to example.com if the subdomain is used for requests other then images .
Replace sub.example.com and example.com with your real domain name in the RewriteCond and RewriteRule above.
If this works and you are happy with the redirect then change R=302 to R=301 to make the redirection permanent.

Redirect users from www.mysite to mysite

We've been issued a new certificate for our website, but unfortunately without an alternative name for the www subdomain. Visiting the TLD without www works just fine.
Redirecting them via .htaccess doesn't work, probably because the initial connection isn't even made.
I've tried using this:
RewriteCond %{HTTPS} =on
RewriteCond %{HTTP_HOST} ^www.mysite$
RewriteRule ^(.*)$ https://mysite/$1 [R]
Is there any way to "force" users away from www or do we need to be issued a new (fixed) certificate for our domain?
Check this redirect (change example.com to your domain):
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule (.*) https://example.com/$1 [R=301,L]

Different domains, different languages, same content, 1 robots.txt

I'm in this situation:
Domains:
www.example.com
www.example.it
that point at the same content in different languages. E.g.:
www.example.com/audi-car.html
www.example.it/audi-auto.html
and I have only one robots.txt in the root domains.
My question is: How can set my robots.txt to disallow crawling of www.example.it to all bots coming from www.example.com and reverse?
Write different robots.txt for each domain and use .htaccess to redirect robots.txt request based on host from where the request came:
RewriteCond %{HTTP_HOST} ^(.*)\.com$ [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^robots\.txt$ /robots-com.txt [L]
RewriteCond %{HTTP_HOST} ^(.*)\.it$ [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^robots\.txt$ /robots-it.txt [L]
Make sure that RewriteEngine On is placed on top and mod_rewrite enabled.

How to make a redirect in .htaccess for pages from different subdomains

I'm not very advanced at programming and I need to make a 301 redirection from one specific subdomain page to another. The tricky part, at least for me, is that the pages are on two different subdomains. So far I've only managed to 301 ALL pages to the target or crashing the whole site.
I've used many, but are currently at:
RewriteCond %{HTTP_HOST} !^spiele.deutschedownloads.de/?DriverScanner/Diverse//download/4770$ [NC]
RewriteRule ^(.*)$ http://programme.deutschedownloads.de/?DriverScanner/Drivers/Werkzeuge-Diverse/download/4413 [L,R=301]
What I want to do is simply to 301 redirect
spiele.deutschedownloads.de/?DriverScanner/Diverse//download/4770
To
http://programme.deutschedownloads.de/?DriverScanner/Drivers/Werkzeuge-Diverse/download/4413
Any help is most appreciated!
This should work:
RewriteCond %{HTTP_HOST} ^spiele\.deutschedownloads\.de$ [NC]
RewriteCond %{QUERY_STRING} ^DriverScanner/Diverse//?download/4770/?$ [NC]
RewriteRule ^ http://programme.deutschedownloads.de/?DriverScanner/Drivers/Werkzeuge-Diverse/download/4413 [R=301,L]
First condition matches the domain in case they are both on the same root folder.
Second condition matches the query string.
If both conditions are OK it redirects.

redirecting just a single non-www homepage to www version, without redirecting any other pages

I need to redirect the non-www version of just a single page, the homepage, to www.
I want to leave the rest of the site alone. Due to a need to keep a specific page non www.
So if i try a rewrite rule of
/index.html http://www.example.com/
I get a infinite redirect type error.
There's probably a way to achieve what you want, although I must say that this has a bit of a "smell" to it. It sounds like you are trying to treat a single symptom rather than fix a problem (which, admittedly might be beyond your control). Is it possible that your organization needs to setup a domain pointer so that www.example.com and examples.com are the exact same thing?
So you want
http://example.com/ to go to http://www.example.com/,
but you want
http://example.com/index.html,
http://example.com/otherstuff, and
http://example.com/subdir/path/file.php
to stay the same?
For Apache:
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^/$ http://www.example.com/ [R=301,L]
If you want any non-www subdomain on the site to get redirected to the www version:
RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
RewriteRule ^/$ http://www.%{HTTP_HOST}/ [R=301,L]
If you want to redirect everything except a specific page:
RewriteCond %{REQUEST_URI} !^/subdir/path/file.php$
RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]