htaccess to add directory after domain name? - .htaccess

How can I use htaccess to always ADD a directory immediately after the domain name?
So for example, change requests for
http://domain.com/path-to/file.php
to
http://domain.com/added-directory/path-to/file.php
The context here is that i am migrating a site to a new server, and the domain name is not yet pointed to the new server. But the hosting company provides me with a "temporary url" based on the Shared IP and my account username, so http://216.172.172.211/~myusername/ , but all the paths in all the html are doc-root relative, like /images/logo.png, which translates to http://216.172.172.211/images/logo.png which is wrong. I need it to be http://216.172.172.211/~myusername/images/logo.png .

try adding these rules to the htaccess file in your document root:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/~myusername
RewriteRule ^(.*)$ /~myusername/$1 [L]
If you want to redirect so that URLs show the ~myusername part in the URL address bar, add an R flag to the square brackets:
RewriteRule ^(.*)$ /~myusername/$1 [L,R=301]

This worked for me perfectly
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/~gye
RewriteRule ^(.*)$ https://%{HTTP_HOST}/gye/$1 [R=301,L]
If an URL comes without the /XYZ
Add https:// at the beginning and /gye/ after the domain.
Testing here really helped: https://htaccess.madewithlove.be/

Related

How to redirect roodomain/addondomain urls to addondomain

My host does not know how to fix this.
I saw in google results URLs that worry me.
For example, I saw rootdomain/addondomain.com/url1.html etc
this happened because google bot was not redirected to addondomain.com/url1.html for example
So I want to redirect all URLs to addondomain.com only
Because this created duplicate content.
My root domain has nothing to do with addon domain...they have a completely different topic....
I already have redirection from addondomain.rootdomain.com to addon domain in htaccess....
but I want to add the new one too...
This is the code I already have
RewriteCond %{HTTP_HOST} ^addon\.root\.org$ [OR]
RewriteCond %{HTTP_HOST} ^www\.addon\.root\.org$
RewriteRule ^(.*)$ "https\:\/\/www\.addon\.com\/$1" [R=301,L]
here is the example with some random domains...
root domain is : bonesroot.com
addon domain is : beeraddon.com
and beerroot.com files are in the folder bones.com/beer on the server
so I want to create immediate redirection from bonesroot.com/beer to beeraddon.com
is that possible or will it affect the server?
this video explains what I want to do
https://www.youtube.com/watch?v=cRm6deeeTVY
and here is the code they recommend
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$ [NC]
RewriteCond %{REQUEST_URI} ^/addonfolder/(.*)$
RewriteRule ^(.*)$ - [L,R=404]
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$ [NC]
RewriteCond %{REQUEST_URI} ^/addonfolder/(.*)$
RewriteRule ^(.*)$ - [L,R=404]
This is the right idea, but it only triggers a 404. To redirect from https://root.example/addon.example/foo to https://addon.example/foo you would need to do it like this:
# Redirect requests to the subdirectory the addon domain points to
RewriteCond %{HTTP_HOST} ^(www.)?root\.example$ [NC]
RewriteRule ^(addon\.example)(?:$|/(.*)) https://$1/$2 [R=301,L]
This assumes that the subdirectory /addon.example is the same as the name of the addon domain, as described initially in your question. (However, for some reason, you have changed this convention later in your question?! *1)
The $1 backreference contains the subdirectory name (the same as the name of the addon domain). The $2 backreference contains the URL-path less the initial slash prefix.
The RewriteCond directive that you previously had that checked against the REQUEST_URI server variable is not required as this check is better performed in the RewriteRule directive itself.
Test first with a 302 (temporary) redirect to avoid caching issues.
*1 If the name of the subdirectory is different to the name of the addon domain then you will need to hardcode this instead. For example:
# Redirect requests to the subdirectory the addon domain points to
RewriteCond %{HTTP_HOST} ^(www.)?root\.example$ [NC]
RewriteRule ^addon-directory(?:$|/(.*)) https://addon.example/$1 [R=301,L]
TIP: Addon domains (cPanel?) don't need to point to subdomains that point to subdirectories off the main domain. They can point anywhere... including areas outside of the main domains document root. This would avoid having to implement these redirects to begin with.
OK I will explain again. I will use fake domains in this case but very similar to my actual domains
The root domain is alter.org
addon domain is numero.com
numero.com files reside inside alter.org/numero/ folder
I want to keep my current redirects which are also
numero.alter.org/foo which redirects to numero.com
what I have in htaccess is this
RewriteCond %{HTTP_HOST} ^numero\.alter\.org$ [OR]
RewriteCond %{HTTP_HOST} ^www\.numero\.alter\.org$
RewriteRule ^(.*)$ "https\:\/\/www\.numero\.com\/$1" [R=301,L]
and I want to add also redirect which redirects
alter.org/numero/foo to numero.com/foo
because I saw one google search result like that and it is duplicate content...Immediately when google bot hits the alter.org/numero/foo it needs to be redirected to numero.com/foo
Please tell me how to add a new redirect to the existing one

Can I use htaccess to redirect requests to the domain name to a subdirectory

I have changed the A record for domainname.com to point to the IP address of subdomain.domainname.com. That's working.
Now, I'd like to change the htaccess so that any requests made to domainname.com are redirected to subdomain.domainname.com/subdirectory
Is this possible? And if so, what's the command please?
You'd be better off using Name based Virtual hosts, but if you want to use a .htaccess file something along the following lines will do the trick:
RewriteCond %{HTTP_HOST} ^domainname.com$ [NC]
RewriteCond %{REQUEST_URI} !^/subdirectory
RewriteRule .* /subdirectory%{REQUEST_URI} [L]

htaccess redirect multiple domains to central domain

Hi if have 3 domain names with a dns that point at one central site with trough a central htaccess file.
I would like to redirect the site by dns to the central site + subdirectory containing the domain name.
for example:
testsite1.com => thesite.com/testsite1/
testsite2.com => thesite.com/testsite2/
testsite3.com => thesite.com/testsite3/
my idea
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.bla1.com$ [AND]
RewriteCond %{HTTP_HOST} !^bla1.com$
RewriteRule ([^.]+)\.com(/[*]) http://bla2.com/$1/$2 [R=301,L]
The thing is, RewriteRule is not applied to a HTTP_HOST, only on a REQUEST_URI.
So You can do this:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www\.)?example.com$
RewriteRule (.*) http://example.com/%{HTTP_HOST}/$1 [L,R,QSA]
Note however, that ".com" will not be stripped from the hostname that is passed in the new path
Btw, "301" is "Moved Permanently". I am not sure You want that, but anyway, it is a common good practice to first use 302 status when redirecting until you're done testing and everything is finalized

How do I fix old links after modifing .htaccess for Joomla installation in a subfolder?

I have my main Joomla installation in a subdirectory. I used to redirect users from www.mysite.com to www.mysite.com/subdir with a 301 so that the live site was entirely dislocated over there.
I don't actually like the fact that all the URL are preceded by the subdirectory /subdir/ (and I also think this is not very good for SEO) so I modified my .htaccess file like this:
RewriteEngine On
RewriteBase /
# Add trailing slash if path does not contain a period or end with a slash
RewriteCond %{REQUEST_URI} !(\.|/$)
RewriteRule (.*) http://www.mysite.com/$1/ [R=301,L]
#Change http://yoursite.com to http://www.mysite.com (Optional)
RewriteCond %{HTTP_HOST} ^mysite.com$
RewriteRule ^/?(.*)$ http://www.mysite.com/$1 [R=301,L]
#Rewrites http://www.mysite.com/subdir to http://www.mysite.com/
RewriteRule ^(.*)$ subdir/$1 [L]
I also edited the configuration file for Joomla! so that now all the links in the site point (correctly) to www.main.com/theirquery and noto to www.main.com/subdir/theirquery
Now, however, all the old links (that have been posted to other webistes, for example) appears to be broken (404): how can I solve this?
I think I have to redirect (301) them to the new subdirectory-free address, that will be (another time) silently redirected with the htaccess I posted.
But I don't know how to do this!
Thank you in advance!
Can you try setting the $live_site parameter in configuration.php? You need to edit it directly rather than through the backend of Joomla.
You need to add this to your htaccess.
RewriteRule ^subdir/(.*)$ /$1 [R=301,NC,L]
This is not possible since the old links where not physical but stored in a database, so redirecting them wouldn't be possible without passing through the database again.

simple subdomain set up for local development (htaccess?)

I am trying to set up a website, domain
examplesproject.co.uk
with a subdomain which is for the moment called
sub.examplesproject.co.uk .
I am with Bluehost and so I have set up the subdomain and I have got the same document root both for examplesproject.co.uk and sub.examplesproject.co.uk but I want content for
sub.examplesproject.co.uk
to actually be located at
examplesproject.co.uk/sub .
So why didn't I set up the document root at examplesproject.co.uk/sub for the sub-domain? Because then, in local development I would need to treat the two domains as completely separate and that would mean no relative urls which seemed silly given that the subdomain folder is just tantalisingly inside the main domain.
However, if someone browsed to sub.examplesproject.co.uk they would get the same content as examplesproject.co.uk, which I don't want, so I set up htaccess rewrite in the root folder like so:
#rewite sub-domain to sub directory
RewriteCond %{HTTP_HOST} ^[www\.]*sub.examplesproject.co.uk [NC]
RewriteCond %{REQUEST_URI} !^/sub/.*
RewriteRule ^(.*) /sub/$1 [L]
That works, however if you browse to examplesproject.co.uk/sub you can still see the content and I don't want to have two locations for the same content. However if I rewrite this sub-directory to show the sub-domain in the browser address, then I create a loop where it keeps feeding round.
As another side-point, I want my main site to be forced to use www, so I also have the following in my root htaccess..
#force add www on main domain
RewriteCond %{HTTP_HOST} ^examplesproject.co.uk$
RewriteRule ^(.*)$ http://www.examplesproject.co.uk$1 [R=301,L]
However, I want to force the sub domain not to have a www infront i.e. http://sub.examplesproject.co.uk and NOT http://www.sub.examplesproject.co.uk. To do this I am trying the following but it doesn't seem to work for other directories within the sub-domain (if that makes sense). Anyhows this is the code which I put in the sub directory (ie at examplesproject.co.uk/sub):
#force remove www on sub-domain
RewriteCond %{HTTP_HOST} ^www.sub.examplesproject.co.uk [NC]
RewriteRule ^(.*)$ http://sub.examplesproject.co.uk/$1 [L,R=301]
Thought I'd mention in case it affects things.
So, my question is, how do I achieve a nice set-up where:
I can use relative URLs for developing and implementing my subdomain.
browsing to http://sub.examplesproject.co.uk shows the content of http://www.examplesproject.co.uk/sub
browsing to http://www.examplesproject.co.uk/sub doesn't duplicate the sub-domain by showing the content (for SEO purposes)
and also
Main domain examplesproject.co.uk is forced to use www - http://www.examplesproject.co.uk.
Sub domain sub.examplesproject.co.uk is force NOT to use www - http://sub.examplesproject.co.uk.
If anyone can help, I would be really grateful. By the way, locally I have set up virtual hosts http://examplesproject and http://sub.examplesproject using wamp and hosts file to replicate the online behaviour.
Thanks alot for reading. Answers/suggestions welcome.
Sorry about that Tim Post! I have put the content in this time! Nice one for looking at this. Hope that this helps someone.
This is the solution that worked for me (thanks to Jim (jdMorgan) at webmasterworld for this - http://www.webmasterworld.com/apache/4254301.htm)..
Put all of these rules, in this order, into the root .htaccess:
# Externally redirect direct client requests for test subdomain subdirectory paths to the test subdomain
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /sub/([^\ ]*)\ HTTP/
RewriteRule ^sub/(.*)$ http://sub.examplesproject\.co\.uk [R=301,L]
#
# Externally redirect all non-canonical, non-blank, non-test-subdomain hostname requests to canonical "www" main domain
RewriteCond %{HTTP_HOST} !^(www\.examplesproject\.co\.uk)?$
RewriteCond %{HTTP_HOST} !^([^.:]+\.)*sub\.([^.:]+\.)*examplesproject\.co\.uk [NC]
RewriteRule ^(.*)$ http://www.examplesproject.co.uk$1 [R=301,L]
#
# Externally redirect non-canonical subdomain hostname requests to canonical test subdomain
RewriteCond %{HTTP_HOST} ^([^.:]+\.)*sub\.([^.:]+\.)*examplesproject\.co\.uk [NC]
RewriteCond %{HTTP_HOST} !^sub\.examplesproject\.co\.uk$
RewriteRule ^(.*)$ http://sub.examplesproject.co.uk/$1 [R=301,L]
#
# Internally rewrite sub-domain requests to subdirectory path
RewriteCond %{HTTP_HOST} ^sub\.examplesproject\.co\.uk$
RewriteCond $1 !^sub/
RewriteRule ^(.*)$ /sub/$1 [L]
Checking THE_REQUEST in the now-first rule prevents the infinite redirection loop problem you encountered.
Note that exact hostnames are now enforced due to the very-careful use of case-sensitivity and anchoring.
Nice one aiit!

Resources