I've been Googling around for .htaccess redirection information, but nothing I find is quite what I'm looking for.
Basically, I want a solution that will take a site example.com and allow you to enter URL's like:
123.example.com
ksdfkjds.example.com
dsf38jif348.example.com
and this would redirect them to:
example.com/123
example.com/ksdfkjds
example.com/dsf38jif348
So basically accept any subdomain and automatically redirect to a folder on the root of the domain with the name of that subdomain.
Try something like this:
# If we're not on http://example.com
RewriteCond %{HTTP_HOST} .+\.example.com
# Add the host to the front of the URL and chain with the next rule
RewriteRule ^(.*)$ ${HOST}$1 [C,QSA]
# Make the host a directory
RewriteRule ^(.*)\.example\.com(.*)$ http://example.com/$1$2 [QSA]
You don't say what should happen to http://foo.example.com/bar?moo - I've made it go to http://example.com/foo/bar?moo
Change the last line if that's not what you want.
If you just want them to be the entrance:
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$
RewriteRule ^ http://example.com/%1 [L,R]
Otherwise:
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$
RewriteRule ^ /%1%{REQUEST_URI} [L]
Related
this is kinda an odd one:
I need my site to do two things (one of which is already working):
if a user tried to access the domain via HTTP:// it is replaced with https:// - this is for SEO in google and to make the user feel more secure -
the site folder that is used to load the website needs to be the subdomain folder of the site
Oddly the second part of this is working and I figured out - however I'm not sure how to merge these two requests:
HTACCSESS
RewriteEngine on
RewriteCond %{HTTP_HOST} ^trippy\.co\.nz$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.trippy\.co\.nz$
RewriteCond %{REQUEST_URI} !update.trippy.co.nz/
RewriteRule (.*) /update.trippy.co.nz/$1 [L]
But I'm not sure how to make the site display as
https://trippy.co.nz/
I have tried:
RewriteEngine On
RewriteCond %{HTTP_HOST} update\.trippy\.co\.nz [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://update.trippy.co.nz/$1 [R,L]
but then the web address displays as: https://update.trippy.co.nz
and I need to remain as https://trippy.co.nz/
Any help here would really great and I know its a odd situation to be in.
THanks,
Wally
...but then the web address displays as: https://update.trippy.co.nz
You would seem to be redirecting to the subdomain itself, not the subdomain's subdirectory, as you appear to be doing in the first rule. You may also be putting the directives in the wrong order - the external redirect needs to go first - otherwise you are going to expose the subdomain's subdirectory, which does not appear to be the intention.
Try the following instead:
RewriteEngine On
# Redirect HTTP to HTTPS
RewriteCond %{HTTP_HOST} ^(www\.)?trippy\.co\.nz [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule (.*) https://%{HTTP_HOST}/$1 [R,L]
# Rewrite all requests to the subdomain's subdirectory
RewriteCond %{HTTP_HOST} ^(www\.)?trippy\.co\.nz [NC]
RewriteRule !^update\.trippy\.co\.nz/ /update.trippy.co.nz%{REQUEST_URI} [L]
No need for the extra condition in the 2nd rule block, as the check can be performed directly in the RewriteRule and use the REQUEST_URI server variable instead of the $1 backreference in the substitution string.
That that R by itself is a temporary (302) redirect. You may want to change that to R=301 (permanent) once you have confirmed this is working OK.
I have a full site setup at mysite.com/2/
I want urls like:
mysite.com/about to redirect to mysite.com/2/about
mysite.com/work/photos to redirect to mysite.com/2/work/photos
I was hoping I could solve this and add the /2 after the domain through the htaccess file and not have to move the whole site up a level.
Try the folowing code if you want redirect
RewriteEngine On
RewriteCond %{REQUEST_URI} !(about|work/photos)/(2)
RewriteRule ^(about|work/photos)/(.*)$ /$1/2/$2 [R=301,L]
If you need only internal redirection change the last line with this :
RewriteRule ^(about|work/photos)/(.*)$ /$1/2/$2 [L]
If you want to change all requests :
RewriteEngine On
RewriteCond %{REQUEST_URI} !/(2)
RewriteRule ^(.*)/(.*)$ /$1/2/$2 [R=301,L]
What I am trying to do is be able to point something like username.domain.com to index.php?url=username. I have been browsing around Google/StackOveflow and have not been able to find anything that will benefit me.
Best Regards.
Add these rules to the htaccess file in your document root:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www\.)?domain.com$ [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain.com$ [NC]
RewriteCond %{REQUEST_URI} !index.php
RewriteRule ^ /index.php?url=%1 [L]
The first condition makes sure the requested host isn't www.domain.com or domain.com, the next line matches and groups the subdomain name, and the rule itself rewrites everything to /index.php with a query string url and the subdomain (backreferenced using %1).
I have been trying to redirect old pages for my old site to the corresponding new ones with permanent redirect.
as well as redirecting www.example.com to example.com
I dont seem to able to do both on the same time
at the moment redirects works for correct links from ex www.example.com/correctlink to example.com/correctlink
but only example.com/Info.aspx is redirected to example.com/about-magento-demo-store and NOT www.example.com/Info.aspx
*Update
I want to remove www. AND redirect 40-50 specific adress to new specific adresses. My problem is that the redirect only works if google has saved the old link without Www. IF google has stored a link including www. then my redirect Redirect permanent example.com/tabid/61/CategoryID/13/ProductID/64/Default.aspx /index.php/solpaneler/re does not function –
*Update
my htaccess looks a bit like this
(with a few lines in the end that where present before I started editing)
(i also tried to ad a line Redirect permanent http://www.example.com/Info.aspx http://example.com/about-magento-demo-store but it does not function)
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule ^(.*)$ http://example.se/$1 [R=301,QSA,L]
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%2://%1%{REQUEST_URI} [L,R=301]
Redirect permanent /Info.aspx /about-magento-demo-store
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteRule .* index.php [L]
Just use rewrite rules for everything. Rewrite rules and Redirect don't always mix well.
RewriteRule ^Info\.aspx$ /about-magento-demo-store [L,R=301]
I've had the same problem a looooooooong time ago. Here's a sample of my rewriterules:
# If domain name doesn't end with .com redirect to .com:
RewriteCond %{HTTP_HOST} (.*)\.(fr|net|org|eu) [NC]
RewriteRule (.*) http://%1.com$1 [R=301,L]
# If domain name without "www", add them:
RewriteCond %{HTTP_HOST} ^mydomainname\.(fr|com|net|org|eu) [NC]
# Ca signifie forcément que c'est sans www => forcer redirection :
RewriteRule (.*) http://www.mydomainname.%1$1 [QSA,R=301,L]
NB: Put this on the top of your rewrite rules, because it should be processed before anything else so that you are sure your domain name always begin with "www"
Note that it redirects mydomainname only if it's "empty" i.e. nothing behind. It won't touch URLs like http://abc.mydomainname.com and it won't touch URLs like http://abc.def.mydomainname.com
Tell me if it works.
I'm trying to create a .htaccess that would for a subdomain "test" redirects all requests this way:
http://test.mydomain.com/something/something2 (1)
to
http://test.mydomain.com/www/something/something2 (2)
so that in browser's address bar is still the address (1)?
I have Apache 2.0.
I'm trying to write it for two hours and I can't still find the right way.
Thanks!
You'll want something like this:
RewriteEngine On
# Only redirect if we're on the subdomain and haven't redirected
# (internally) to /www/ yet
RewriteCond %{HTTP_HOST} ^test
RewriteCond %{REQUEST_URI} !^/www/
RewriteRule ^.*$ /www/$0 [L]
# Let's also prevent them from being able to go to our /www path manually
RewriteCond %{THE_REQUEST} ^(POST|GET)\s/www
RewriteRule ^www/?(.*)$ http://test.mydomain.com/$1 [R,L]