.htaccess redirect to a single subdomain - apache

I have a website like www.example.com, I want to redirect each hit to this URL and Sub URL to a single URL on subdomain. Following are some examples
www.example.com should be redirected to http://test.example.com
www.example.com/show/mypage1 should be redirected to http://test.example.com
www.example.com/show/mypage2 should be redirected to http://test.example.com
www.example.com/show/mypage3 should be redirected to http://test.example.com
I want to do it using .htaccess. and I want 302 temporary redirect. I am using Apache WebServer.

Are you using Apache or IIS?
In Apache:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://test.example.com/ [L,R=302]
edited
edited: replaced http://test.example.com/$1 to http://test.example.com/, it works now as required.

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://test.example.com/ [L,R=302]
</IfModule>

Related

How to fix not found error when redirect to https://www is used with RewriteBase?

I want any access to my website to redirect to https://www. but I have a RewriteBase.
Suppose my website is example.com, I want:
example.com to redirect to https://www.example.com
http://example.com to redirect to https://www.example.com
https://example.com to redirect to https://www.example.com
https://www.example.com to redirect to https://www.example.com
And my website files are located in a subfolder, so I have a RewriteBase which is for example:
RewriteBase /~myuser/mysite
My .htaccess file contains:
RewriteEngine On
RewriteBase /~myuser/mysite
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
I referred to the following question to write my .htaccess file: htaccess redirect to https://www
As a result, if I write on in a browser a url such as example.com/page1 (which is a valid page), it redirects to https://www.example.com/page1 however, I get the following error:
Not Found
The requested URL /page1 was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
I think the problem is caused by the RewriteBase, but I do not know how to fix it.
Note that when I access the website main url https://www.example.com all internal sub-links work correctly.
Here is the solution
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteBase /~myuser/mysite
Simply I placed the RewriteBase at the end after the RewriteRule and now it works.

redirecting http to https and www to non www on apache

I need to set advanced (for me) redirect based (hopefully) on our apache2 and site .htaccess.
We want that anyone who will point www.example.com and example.com or https://www.example.com would have been redirected to https://example.com
The issue is that links after domain must work.
For example http://example.com/category/article/newest and https://www.example.com/category/article/newest would work after that redirection and pointing to correct https://example.com/category/article/newest
We tried redirection as you can see in attached .htaccess but problem is that it is for example redirecting http://www.example.com/category/article/newest to https://example.com/
our current .htaccess
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\.$ [NC]
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteRule .* public/index.php
SetEnv APPLICATION_ENV development
<IfModule mod_php.c>
php_flag magic_quotes_gpc off
php_flag register_globals off
</IfModule>
Try changing your rules to:
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\.$ [NC]
RewriteRule .* https://example.com%{REQUEST_URI} [R=301,L]
This way, if the request is not HTTPS or if the request starts with a "www", it will get redirected to HTTPS and without the "www".

Need some assistance with redirects in .htaccess

My problem is that both non-HTTPS requests and requests with WWW redirects to Main_Page, which is fine when the requested url is the root domain, but not if the request is an article, special page etc.
Example 1: http:// example.com/wiki/Articlename redirects to https:// example.com/wiki/Main_Page, but I want to redirect to https:// example.com/wiki/Articlename
Example 2: https:// www.example.com/wiki/Articlename redirects to https:// example.com/wiki/Main_Page, but I want to redirect to https:// example.com/wiki/Articlename
This is my settings in .htaccess:
<IfModule rewrite_module>
RewriteEngine On
RewriteBase /
Options +FollowSymlinks
# Force HTTP to HTTPS/SSL
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://example.com/$1 [R,L]
# Redirect Example.com to Example.com/wiki/Main_Page
RewriteCond %{HTTP_HOST} example\.com [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ /wiki/$1 [PT,L]
# Short url for wiki pages
RewriteRule ^/?wiki(/.*)?$ %{DOCUMENT_ROOT}/w/index.php [PT,L]
# Permanent redirect www url to non-www
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule (.*) http://example.com/$1 [L,R=301]
</IfModule>
Using MediaWiki on a shared host (Debian 8 Jessie, Apache, PHP 5.6), so httpd.conf is not an option. Example.com only serves MediaWiki and that is why the domain is redirected to https:// example.com/wiki/Main_Page (default).
Any idea what I'm doing wrong?
Edit: HTTPS rewrite is now working after moving "# Force HTTP to HTTPS/SSL" to the top in .htaccess, but WWW-rewrite still does not work. Code is updated.
i think your problem lies with your use of 'L' after RewriteRule - http://httpd.apache.org/docs/current/rewrite/flags.html#flag_l
it causes rule processing to cease, while it seems your logic requires several rules to execute

apache2 redirect http to https with full url

I have this rule:
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://www.dnilabs.at/$1 [R=301,L]
So when i browse to the url http://www.dnilabs.at/loesungen/ redirects to https index.php and should be https /loesungen/
What do i miss here? shouldn't it be redirected to /loesungen/ instead of /index.php
I also tried this with the same result
^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI}
OK it works, my problem was i placed the rules on the end of my .htaccess file, i placed it directly under RewriteEngine On and now it works

Domain .htaccess redirect issue

I'm trying to create some redirects with .htaccess but I never manage to get it fully functional. Maybe someone here can help me.
What I need is:
http://domain.se and http://domain.com to redirect to http://www.domain.com.
I also need http://domain.se/somefolder, http://domain.com/somefolder as well as http://www.domain.se/somefolder to redirect to http://www.domain.com/folder.
I've tried to accomplish this myself but all I end up with is errors about data not being sent.
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
# folder rewrite
RewriteRule ^somefolder$ folder [L]
# domain redirect
RewriteCond %{HTTP_HOST} =domain.com [OR]
RewriteCond %{HTTP_HOST} =domain.se
RewriteRule (.*) http://www.domain.com/$1 [R=301,L]
This is to be placed in .htaccess file in website root folder. If placed elsewhere some tweaking may be required.
First rule will rewrite (internal redirect) requests to /somefolder to /folder. If you need this to be 301 Permanent Redirect, then replace [L] by [R=301,L]
Second rule will do domain redirect job. This rule will ONLY redirect if domain is domain.com or domain.se. If you want to have redirect from ANY domain name (that your webserver is configured can serve) to www.domain.com then replace those 2 RewriteCond lines with this one: RewriteCond %{HTTP_HOST} !=www.domain.com.
RewriteCond %{HTTP_HOST} !^www.domain.com$
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301]
That should meet all of your requirements. All requests that are not www.domain.com will be redirected to that domain, with the request URI intact.