Need some assistance with redirects in .htaccess - apache

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

Related

www doesn't redirect to non www with https on

My problem is when I type example.com or www.example.com in browser it redirect to https://example.com but when I type https://www.example.com, it doesn't redirect to https://example.com.
I want whatever may be the url http or https and www or non www, it always point to https://example.com.
I have used below url rewrite for htaccess file.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]
Thanks in advance.
With your shown samples, could you please try following. Please make sure you clear your browser cache before testing your URLs.
Also make sure this should be your TOP MOST rule in your .htaccess file.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.*) [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [NE,R=301,L]
This should redirect urls irrespective of either url has http or https OR url has www or no www in it TO https without www.

How to prevent homepage 301 chains?

I'm trying to make all versions of homepage URLs 301 to same place, without a 301.
It's difficult to show the problem because I don't have enough rep points to post image or show the http response codes in a chart (too many links!)
But, using the code below https://www.example.com/ goes to http://www.example.com/ first, before it 301s to the homepage URL http://example.com
I'm on apache, and I've been using .htaccess to try to resolve this.
I've tried also tried the following, but it only works for file paths.
Redirect 301 /oldfile.htm /newfile.htm
# Redirect HTTPS to HTTP
RewriteCond %{HTTP:X-Forwarded-Proto} =https
RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#Force non-www:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ http://example/$1 [L,R=301]
I've tried also tried the following, but it only works for file paths.
Redirect 301 /oldfile.htm /newfile.htm
Is there a way I can make https://www.example.com/ go to http://example.com without the 301 chain?
Thanks,
Mike.
You can use a single rule to redirect your https URLs to http and non-www version. This will redirect all of your https urls to http without creating multiple redirect chain.
Replace your htaccess rules with this :
# Redirect HTTPS to HTTP and non-www
RewriteCond %{HTTP:X-Forwarded-Proto} =https [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^(.*)$ http://%1%{REQUEST_URI} [L,R=301]
Make sure to clear your browser cache or use a different web browser to test this rule.

Redirect only HTTP subdomain to HTTPS subdomain in htaccess

How do you redirect only the HTTP subdomain to HTTPS subdomain in .htaccess? The main site is in WordPress and already redirected to HTTPS with a plugin, but the subdomain is a PHP created site. I have looked and not seen a conclusive solution to this on here.
I copy and pasted this suggested code but it did nothing:
#Redirect Subdomain
RewriteEngine on
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com$ [NC]
RewriteRule .? https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteEngine on
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com$ [NC]
RewriteRule .? https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
This would create a redirect loop (if executed at all). In order to redirect from HTTP to HTTPS you need to first check you are not already on HTTPS (otherwise you will get a redirect loop).
These directives would need to go in the .htaccess in the document root of your subdomain. Or at the top of your root (WordPress) .htaccess file if the subdomain points to the document root of the main site. The important thing is that the redirect must go before the WordPress front-controller.
This also assumes your SSL cert is installed directly on your application server and not a proxy.
Try the following:
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com [NC]
RewriteRule .? https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
This checks that HTTPS does not contain "on" and the subdomain is being requested before redirecting to HTTPS on the same host.
Although if WP is simply redirecting the main domain, then you could do all this in .htaccess and simply remove the condition that checks against HTTP_HOST. Although if you have other subdomains that should not be redirected to HTTPS then alter the CondPattern to match just the subdomain or main domain (and www subdomain). For example:
RewriteCond %{HTTP_HOST} ^((subdomain|www)\.)?example\.com [NC]
Please try one of the followings:
you need to edit
Redirect "/" "https://yourwebsite.com/"
OR
you dont need to edit following
RewriteEngine on
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^/(.*) https://%{HTTP_HOST}/$1 [NC,R=301,L]

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.

.htaccess redirect to a single subdomain

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>