Rewrite/redirect urls to subdomain - regex

I have an add-on on domain that is finally registered and can be accessed without the main domain. The add-on domain have been used like this: (I didn't find similar domain setup to help me out with this)
primary.com/secondary.de/
primary.com/secondary.de/files
primary.com/secondary.de/files/file
I'd like to redirect users to:
secondary.de/
secondary.de/files
secondary.de/files/file
addresses. What expressions should I use in my htacces file?
Thanks

Since this is WP install, put this code in your .htaccess under DOCUMENT_ROOT/secondary.de directory:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?primary\.com$ [NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(secondary\.de/[^\s]*) [NC]
RewriteRule ^ http://%1 [NE,R=301,L,NC]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

Related

Redirect all wild card subdomains and subdomain urls to the root domain and root domain urls using htaccess

I'm trying to redirect my subdmain in this manner: anysubdomain.my_domain.com and its URLs must be redirected to root domain and its URLs respectively.
For example (1) if a user tries http://anysubdomain.my_domain.com it should redirect tohttps://www.my_domain.com.
And (2) if a user tries http://anysubdomain.my_domain.com/anyurl it should be redirected to https://www.my_domain.com/anyurl.
I was able to achieve (1) using below htaccess code:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteCond %{HTTP_HOST} !^www.my_domain.com$
RewriteRule ^(.*)$ https://www.my_domain.com/$1 [QSA,L,R=301]
</IfModule>
yet I could not do the (2) part. I'm sure we can do this with a modification for above htaccess code, but I don't know how.
I already have wildcard cname, that is why (1) perfectly working.
Also it is a WordPress site, if that matters.
Put these rules first :
RewriteCond %{HTTP_HOST} !^www.my_domain.com$
RewriteRule ^(.*)$ https://www.my_domain.com/$1 [QSA,L,R=301]
like this :
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www\.)?my_domain\.com$
RewriteRule ^(.*)$ https://www.my_domain.com/$1 [QSA,L,R=301]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Note: clear browser cache the test.
You may use this .htaccess to handle all subdomains:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} (?:^|\.)(my_domain\.com)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [NE,L,R=301]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
</IfModule>
Make sure to test it from new browser to clear browser cache fully.

Redirect subdomain to a specific folder only without the main domain

I have links like this one:
a.site.co.il
b.site.co.il
...
I configured the server to work with wild cards and redirect these subdomains into a folder on this IP:
62.90.154.000/goin
Then, I created a htaccess on 62.90.154.000 root so using of subdomain will redirect into a specific folder.
An example of this would be:
a.site.co.il => 62.90.154.000/goin
But the Ip will not redirect into goin folder, but to the IP itself:
62.90.154.000 => 62.90.154.000
Here is my htaccess:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.+)$ /$1/ [R=301,L]
RewriteRule ^(!62?|.*)$ goin/index.php?subdomain=$1 [L]
This seems to work, but the problem is that this htaccess also redicreds the main domain (62.90.154.000) into the subdomains' folder!
How can I solve this?!
This rule:
RewriteRule ^(!62?|.*)$
Is just skipping REQUEST_URI that start with /62. It is not skipping your IP address.
You can use:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
# skip below rule for IP addresses that start with 62.
RewriteCond %{REMOTE_ADDR} ^62\. [OR]
RewriteCond %{HTTP_HOST} ^62\.
RewriteRule ^ - [L]
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301,NE]
RewriteCond %{HTTP_HOST} ^(?!www\.)[^.]+\.site\.co\.il$ [NC]
RewriteRule .* /goin/$0 [L,NE]
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.*)$ goin/index.php?subdomain=$1 [L,QSA]
Make sure to completely clear your browser cache before testing this change.

htaccess redirect all subfolders/pages on one domain to matching subfolder on new domain (WordPress)

I have a bunch of domains that all go to the same site and need to be able to redirect domainalias.com/folder/name to website.com/folder/name.
I currently have the following htaccess which works as far as redirecting the root domain, but as soon as I go into a sub-directory or sub-page, it won't redirect:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# BEGIN WordPress
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress
# Redirect all domains to website.co.uk
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} !^website.co.uk$ [NC]
RewriteRule ^(.*)$ http://website.co.uk/$1 [L,R=301]
</IfModule>
So to confirm, I want people to be redirected from http://anydomainalias.co.uk/any/sub/folder to http://website.co.uk/any/sub/folder. As it's a WordPress site, I need this to work dynamically. I have 50-odd subdomains so want to avoid having a rule for each one as well (hence the !^website.co.uk rule).
Thanks.
Have redirect rule before default WP routing rule:
<IfModule mod_rewrite.c>
RewriteEngine On
# Redirect all domains to website.co.uk
RewriteCond %{HTTP_HOST} !^website\.co\.uk$ [NC]
RewriteRule ^(.*)$ http://website.co.uk/$1 [L,R=301]
# BEGIN WordPress
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress
</IfModule>
Make sure to test it after clearing your browser cache.

URLs without www all redirect to home page

What do I need to do to redirect non-www URLs to their corresponding pages with www in the URL?
For example, there's a page called www.mysite.com/contactus. If try to access this site via mysite.com/contactus, it simply redirects to www.mysite.com.
Basically, I'd like all mysite.com/* to go to www.mysite.com/* instead of just going to the home page. Is this an htaccess configuration? Below is what I have for the mod_rewrite. Let me know if more information is required.
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{REQUEST_URI} !^/(media|skin|js)/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* index.php [L]
</IfModule>
Insert this rule just below RewriteBase:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [NE,R=301,L]

Remove 'index.php' from URL with .htaccess

I've been trying all sorts of solutions from this site and none seem to work. I'm currently hosting with hostgator. This is my current .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
<IfModule mod_suphp.c>
suPHP_ConfigPath /home/user/php.ini
<Files php.ini>
order allow,deny
deny from all
</Files>
</IfModule>
This is in the root folder of my site. I have also tried adding a ? after index.php and no luck. Does anyone know why this isn't working?
This is the code you can use in your .htaccess (under DOCUMENT_ROOT) to remove index.php from URI:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L,QSA]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
RewriteRule ^ %1 [R=301,L]
Symfony 2 has an excellent solution:
RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
# Sets the HTTP_AUTHORIZATION header removed by apache
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^index\.php(/(.*)|$) %{ENV:BASE}/$2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .? - [L]
RewriteRule .? %{ENV:BASE}/index.php [L]
This accomplishes the following:
RewriteBase is not necessary (useful when the site is in a subdirectory beneath the web root)
index.php is removed if present
The request is routed to the correct index.php with the full query string from the original request
Note that the line:
RewriteRule ^(.*) - [E=BASE:%1]
is responsible for setting the %{ENV:BASE} variable for later on. Refer to Apache documentation on E|env flag.
I tried this and works fine:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# Removes index.php from ExpressionEngine URLs
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteCond %{REQUEST_URI} !/system/.* [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
# Directs all EE web requests through the site index file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
Exceptions
If your site’s system directory (/system/) has been renamed and is still accessible by URL, modify the RewriteCond line above:
RewriteCond %{REQUEST_URI} !/newdirectoryname/.* [NC]
If you are running EE from a sub-directory rather from the root of your domain (e.g. http://example.com/myeesite/ instead of http://example.com/), just remove the slash preceding index.php in the RewriteRule line above, like so:
RewriteRule ^(.*)$ index.php/$1 [L]
If you are running EE from a sub-directory and it still doesn’t work after removing the slash, you may need to specify the sub-directory in your rewrite rule. For example, if your sub-folder is named testing, change:
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
To:
RewriteRule (.*?)index\.php/*(.*) testing/$1$2 [R=301,NE,L]
And change:
RewriteRule ^(.*)$ /index.php/$1 [L]
To:
RewriteRule ^(.*)$ testing/index.php/$1 [L]
If your host requires forcing query strings, try adding a question mark following index.php in the RewriteRule line above, like so:
RewriteRule ^(.*)$ /index.php?/$1 [L]
To remove index.php from urls on apache2.4 you can use the following rule :
RewriteEngine on
RewriteRule ^index\.php/(.+)$ /$1 [L,R]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule !index\.php /index.php%{REQUEST_URI} [L,END]
This will change the uri
/index.php/foobar
to
/foobar
This rule will return an internal server error for lower versions of apache as they don't support the END flag. Please see the anubhava's answer above that works almost on all versions.
I've been compelled to join stack overflow.com today to comment here as the answer has solved a long term problem I've had with a Zend Framework website. I've worked on the website around 3 1/2 years and during that time I discovered that it didn't handle index.php correctly and this causes webmaster tools to see duplicate titles etc.
I decided to search again today for a solution, one of many times attempted.
This is the one (the last answer shown above) that solves my problem.
# Removes index.php from ExpressionEngine URLs
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteCond %{REQUEST_URI} !/system/.* [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
As an example. this is what it achieves for me.
http://www.domainname.co.uk/index.php/categories/url-name.html
becomes
http://www.domainname.co.uk/categories/url-name.html
Thank you to everyone who contributed to the original question as it lead to the answer and solution above.
Extra Note: I have other rewrite commands that handles the other aspects but those on their own didn't fix the index.php and the only time this has been fixed is by using.
# Removes index.php from ExpressionEngine URLs
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteCond %{REQUEST_URI} !/system/.* [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
I only hope it helps others whether its ExpressionEngine or Zend Framework, in the future.