Forum Moderators: phranque
I don’t want to use a WordPress plugin to do it but .htaccess file in Apache.
I’ve gone over lots of tutorials for .htaccess and have never seen any of them that does both a www non secure to https redirect, AND an http:// no www to https redirect AND different keywords in the new URL after the domain name
[edited by: penders at 11:37 pm (utc) on Aug 7, 2019]
How to I write the code for moving both the www and the non www insecure urls to https and totally different keywords in the htaccess file and not have chains of redirects?
...
Does it matter if I do the http: //www.site.com/keyword-abc.html first before the http: //site.com/keyword-abc.html in the redirect?
RewriteRule ^old-keyword\.html$ https://www.example.com/new-keyword/ [R=301,L] If I put a space between each URL change to be able to see and trouble shoot it easier, will that affect the speed of the redirects or cause any problems?
If I put a space between each URL changeBy “space” do you mean a blank line? In configuration files--including htaccess--unlike robots.txt, blank lines have no syntactic meaning. You use them purely to preserve your own sanity and to make the file more readable. In addition to leaving blank lines, it's a very good idea to include
# Needed before any rewriting
RewriteEngine On
### Place after 'RewriteEngine On' and before any CMS specific rewrite rules
# Redirect HTTP with www to HTTPS without www
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule .* https://%1%{REQUEST_URI} [R=301,L]
# Redirect HTTP without www to HTTPS without www
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Redirect HTTPS with www to HTTPS without www
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule .* https://%1%{REQUEST_URI} [R=301,L] [edited by: phranque at 5:51 am (utc) on Aug 13, 2019]
[edit reason] unlinked urls [/edit]
The code.
# Needed before any rewriting
RewriteEngine On
### Place after 'RewriteEngine On' and before any CMS specific rewrite rules
# Redirect HTTP with www to HTTPS without www
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule .* https://%1%{REQUEST_URI} [R=301,L]
# Redirect HTTP without www to HTTPS without www
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Redirect HTTPS with www to HTTPS without www
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule .* https://%1%{REQUEST_URI} [R=301,L]
# Redirect non-HTTP or non-www to HTTPS without www
RewriteCond %{HTTPS} =off [OR]
RewriteCond %{HTTP_HOST} !^(example\.com)?$ [NC]
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L] ## 301 Redirects
# 301 Redirect 1
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^addintheworkplacearticles\.html$ https://example.com/manage-adhd-at-work/? [R=301,NE,NC,L]
# 301 Redirect 1
RewriteRule ^addintheworkplacearticles\.html$ https://example.com/manage-adhd-at-work/? [R=301,NC,L] [edited by: phranque at 6:52 am (utc) on Aug 24, 2019]
I’ve read a lot about http to https and a fair bit about .htaccess and did not read anything about that.
How would I place the 301 redirects in front of the WordPress ones? Just paste it at the top of the htaccess file?
But I don’t really understand his explanation.You have no idea how tempting it is to say “Don’t trouble your pretty little head about how or why; just do what phranque says.” If it were not past my bedtime I would give a fuller explanation, possibly including a discussion of why some parts of Redirect Coding Tool Guy’s own explanation are simply irrelevant. (Do you even have multiple domains sharing the same htaccess file?)
I just use plain html url’s. I don’t do advertising, only do organic SEO, no UTM parameters in any of my URLs. I don’t send ezine with UTM parameters.Uh... what does any of this have to do with the mechanics of redirection? And where did “UTM parameters” come from?
“Why don't I use the shorter Redirect command ...
The use of RewriteRule to perform this task may be appropriate if there are other RewriteRule directives in the same scope. This is because, when there are Redirect and RewriteRule directives in the same scope, the RewriteRule directives will run first, regardless of the order of appearance in the configuration file.
"(Do you even have multiple domains sharing the same htaccess file?)"
No I don't. Is that what his code is used for? vs the normal individual redirect?
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule .* https://%1%{REQUEST_URI} [R=301,L]
# 301 Redirect 1
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^addintheworkplacearticles\.html$ https://example.com/manage-adhd-at-work/? [R=301,NE,NC,L]
# 301 Redirect 2
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^adhdmedicationart\.html$ https://example.com/adhd-medication-articles/? [R=301,NE,NC,L]
...
# Redirect non-HTTP or non-www to HTTPS without www
RewriteCond %{HTTPS} =off [OR]
RewriteCond %{HTTP_HOST} !^(example\.com)?$ [NC]
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
# 301 Redirect 1
RewriteRule ^addintheworkplacearticles\.html$ https://example.com/manage-adhd-at-work/? [R=301,NC,L]
[edited by: phranque at 6:54 am (utc) on Aug 24, 2019]
[edited by: phranque at 6:54 am (utc) on Aug 24, 2019]
[edit reason] fix typo [/edit]
looks like I might be risking redirect chains and page rank damage if I use mod_rewrite. Or at least with double redirects, slowing things down. So no need for me to use that. Best to stir to regular redirects.
#3 Because of #1, any directives for mod_alias (Redirect by that name) will always execute after directives for mod_rewrite (RewriteRule, including but not limited to redirects).
[edited by: phranque at 6:55 am (utc) on Aug 24, 2019]
[edit reason] fix typo [/edit]
then after that code, put the WordPress htaccess code?Yup. Make sure you leave the WP comment lines--not2easy probably knows the exact formula--because that’s what WP looks for whenever you upgrade.
[edited by: phranque at 6:55 am (utc) on Aug 24, 2019]
[edit reason] fix typo [/edit]