Topic on Manual talk:Short URL/Apache

Need some help with redirects in .htaccess

3
Summary by Berot3

didn't reply

Fredrilj (talkcontribs)

My problem is that both none HTTPS requests and requests with WWW redirects to main page, whitch is fine when the requested url is the root domain, but not if the request is an article, special page etc.

Example 1: http://mydomain.com/wiki/Articlename redirects to https://mydomain.com/wiki/Main_Page, but I want to redirect to https://mydomain.com/wiki/Articlename

Example 2: https://www.mydomain.com/wiki/Articlename redirects to https://mydomain.com/wiki/Main_Page, but I want to redirect to https://mydomain.com/wiki/Articlename

This is my settings in .htaccess:

<IfModule rewrite_module>
RewriteEngine On
Options +FollowSymlinks

# Redirect Mydomain.com to Mydomain/wiki/Main_Page
RewriteCond %{HTTP_HOST} mydomain\.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\.mydomain\.com$ [NC]
RewriteRule (.*) http://mydomain.com/$1 [L,R=301]

# Permanent redirect HTTP to HTTPS
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://mydomain.com/$1 [R=301,L]
</IfModule>

Any idea what I'm doing wrong?

Fredrilj (talk) 10:22, 17 April 2017 (UTC)

Robertinventor (talkcontribs)

Probably that you need to put the http to https redirect at the start rather than the end of the file - I haven't studied how .htaccess works - but if https is working fine then if you convert the http to https right at the start, the rest of the url unchanged, then as it flows through it should work just as if the user had entered an https url originally. Robertinventor (talk) 16:52, 1 October 2018 (UTC)

Stevio Bee (talkcontribs)

Make sure the 'rewrite' module is on first - had me stuck for ages.... it is off by default on Apache


'sudo a2enmod rewrite'

then restart:

'sudo systemctl restart apache2'

Reply to "Need some help with redirects in .htaccess"