Welcome to WebmasterWorld Guest from 34.204.203.142

Forum Moderators: Ocean10000 & phranque

How to do 301’s with new keywords in URLs + redirect www to https?

     
10:50 pm on Aug 7, 2019 (gmt 0)

New User

Top Contributors Of The Month

joined:Aug 7, 2019
posts: 31
votes: 0


I’m moving a 180 page html non https site over to WordPress and to https AND changing the keywords of the permalinks after the domain name in the URLs in an .htaccess file in Apache. I'm keeping the root domain name the same, my new site is now on a test server.

Some of the old files I'm moving are http: //www.site.com/keyword-abc.html

AND sadly some of the old files are duplicates of those files but not www. I.e., http: //site.com/keyword-abc.html

So I need to manually find some way to list both the html files http: //www.site.com/keyword-abc.html

And the

http: //site.com/keyword-abc.html

And redirect it not only to https but also change the URL after the domain name like

https: //site.com/new-keyword-that-ranks/

I don’t want to use a WordPress plugin to do it but .htaccess file in Apache.

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?

I know I will have to write a line of code or two for each URL I want to change.

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?

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?

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

I think the hard part is the different keywords after the domain name but I've already built the site with those URLs in it.

Thanks
6:45 am on Aug 16, 2019 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:11872
votes: 245


I think I want mod_alias 301 redirects.

for at least the third time from me, no you don't.
7:36 pm on Aug 16, 2019 (gmt 0)

New User

Top Contributors Of The Month

joined:Aug 7, 2019
posts: 31
votes: 0


OK thanks I understand. Does the last code I posted achieve my goal stated in the post?
8:08 pm on Aug 16, 2019 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:15934
votes: 889


Yes, it looks as if you're solid on mod_rewrite now.
9:45 pm on Aug 16, 2019 (gmt 0)

New User

Top Contributors Of The Month

joined:Aug 7, 2019
posts: 31
votes: 0


Great! Thank you all so much for the help. I really appreciate it.
5:42 am on Aug 23, 2019 (gmt 0)

New User

Top Contributors Of The Month

joined:Aug 7, 2019
posts: 31
votes: 0


Hi, I tested the code out in 3 htaccess validators and all had some problems. they showed what area of the code was the problem but I don't know what is the specific code fragment in the code they highlighted that needs to be fixed.

Tried the made with love htaccess tester. On this code (it only tests a single redirect). Here was the code I tried (from the one listed here.)

# 301 Redirect 1
RewriteRule ^addintheworkplacearticles\.html$ https://example.com/manage-adhd-at-work/ [R=301,L]


# Redirect non-HTTP or non-www to HTTPS without www
RewriteCond %{HTTPS} =off [OR]
RewriteCond %{HTTP_HOST} !^(example\.com)$?
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]

and got the right output, but got these errors.

Output url
https://example.com/manage-adhd-at-work/

Debugging info

1. RewriteRule ^addintheworkplacearticles\.html$ https://example.com/manage-adhd-at-work/ [R=301,L]

The new url is https://example.com/manage-adhd-at-work/
Test are stopped, a redirect will be made with status code 301

2. RewriteCond %{HTTPS} =off [OR]

This rule was not met.

3. RewriteCond %{HTTP_HOST} !^(example\.com)$?

Invalid line:We failed to execute your regular expression, is it valid?

4. RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]

This rule was not met.

Do you know what was the specific errors in the code? The tool did not specify. I checked two other htaccess testing tools and also go errors. But unlike this tool, the each showed only one error and different ones. Will post the results in separate posts. I couldn't find any other htaccess testing tools.
5:49 am on Aug 23, 2019 (gmt 0)

New User

Top Contributors Of The Month

joined:Aug 7, 2019
posts: 31
votes: 0


The other tool I tried was called .htaccess check. I posted the same code.

# 301 Redirect 1
RewriteRule ^addintheworkplacearticles\.html$ https://example.com/manage-adhd-at-work/ [R=301,L]

# Redirect non-HTTP or non-www to HTTPS without www
RewriteCond %{HTTPS} =off [OR]
RewriteCond %{HTTP_HOST} !^(example\.com)$?
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]

and here's their result.

Test Results

​1 # 301 Redirect 1
2 RewriteRule ^addintheworkplacearticles\.html$ https://example.com/manage-adhd-at-work/ [R=301,L]
3
4 # Redirect non-HTTP or non-www to HTTPS without www
5 RewriteCond %{HTTPS} =off [OR]
6 RewriteCond %{HTTP_HOST} !^(example\.com)$?
7 RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]

They flagged line 6. they said
Fatal: RewriteCond cannot compile regular expression ‘!^(example\.com)$?’
5:53 am on Aug 23, 2019 (gmt 0)

New User

Top Contributors Of The Month

joined:Aug 7, 2019
posts: 31
votes: 0


3rd tool I tried was Basic .htaccess code syntax checker and validator.

I pasted the same code

# 301 Redirect 1
RewriteRule ^addintheworkplacearticles\.html$ https://example.com/manage-adhd-at-work/ [R=301,L]

# Redirect non-HTTP or non-www to HTTPS without www
RewriteCond %{HTTPS} =off [OR]
RewriteCond %{HTTP_HOST} !^(example\.com)$?
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]

Their result

Lyxx found 1 problem in the code segment below.

line 5: # Redirect non-HTTP or non-www to HTTPS without www
line 6: (20) RewriteCond %{HTTPS} =off [OR]
line 7: RewriteCond %{HTTP_HOST} !^(example\.com)$?

The problem was line 6 line 6: (20) RewriteCond %{HTTPS} =off [OR]

Thanks
1:49 pm on Aug 23, 2019 (gmt 0)

Administrator from US 

WebmasterWorld Administrator not2easy is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Dec 27, 2006
posts:4558
votes: 363


Testing .htaccess outside of the environment where it will be used is subject to errors. IF the testing tool assumes some configuration that is not the same as your server's configuration it is a "best guess" tool. What do these tools show you when you test the .htaccess file you have been using? I've never used a tool to test a .htaccess file so I am curious.

What is that (20) that Lyxx shows at "Line 6"? Is that the
RewriteCond %{HTTPS} =off [OR]
Asking because it may depend on something we can't see here - it is commonly written as
RewriteCond %{HTTPS} off [OR]


Also are you sure that
!^(example\.com)$?
is what you want?
!^(example\.com)?$
is more common and may be why the tool is gagging?

2:30 pm on Aug 23, 2019 (gmt 0)

Senior Member

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2006
posts: 3153
votes: 7


!^(example\.com)$?


The trailing "?" is certainly the problem here - the regex is invalid, as the preceding "$" (end-of-string anchor) cannot be "optional" (it's not quantifiable).

You have the last two characters reversed, as not2easy has pointed out.

But if you are on a shared server then you don't need to make the hostname optional - as it will never be omitted (if it gets to your site at all). The trailing "$" can also be omitted, in order catch FQDN (that end in a dot). For example:

RewriteCond %{HTTP_HOST} !^example\.com


RewriteCond %{HTTPS} =off [OR]


There is no error on that line. Did Lyxx not report what the error actually was? My guess would be a limitation in the tool. "=off" is not a regex. The "=" operator makes this an exact match lexicographical string comparison (although it's still a valid regex, so not sure why Lyxx would report an error here? But why is the line numbering +1 out with this tool?).

Tried the made with love htaccess tester. On this code (it only tests a single redirect).


You don't actually state the URL you are requesting, but if you are requesting "/addintheworkplacearticles.html" (as suggested by the output) then that is correct - only the first directive will be "tested" and processing stops (because of the "L" flag). The remainder of the results from the "made with love" tool are not errors, except for the line that says "Invalid line:We failed to execute your regular expression".

------

If you tried to run this code on a real server, you'd get a 500 Internal Server Error reported in the browser (no redirect) and the following error reported in the servers error log:

....[core:alert].... RewriteCond: cannot compile regular expression '^(example\.com)$?'
3:23 pm on Aug 23, 2019 (gmt 0)

Senior Member

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2006
posts: 3153
votes: 7


Lyxx found 1 problem in the code segment below.
The problem was line 6 line 6: (20) RewriteCond %{HTTPS} =off [OR]


I just had a little play with the Lyxx tool and it just seems to be a strange bug with the tool. Changing "=off" to "off" makes no difference. (You can get rid of the error by making some nonsensical changes, but it doesn't really make sense.) And it doesn't catch the regex error on the next line either.
4:17 pm on Aug 23, 2019 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:15934
votes: 889


Short version: Aside from the minor points noted by penders and not2easy, your “errors” are not errors. The validators are simply telling you which rules will execute and which ones won’t, and what the target will be in the case of rules that do execute.

The business about “cannot evaluate” makes me think of when I run the CSS validator on a local file, and it throws spurious errors because it can’t import external files (the ones in <link rel> statements). But the error may go away once you get the question mark in the right place--or simply delete the bit that makes hostname optional.

The trailing "$" can also be omitted, in order catch FQDN (that end in a dot).
In order to catch say-what-now? The usual reason for ending in $ is to exclude requests that come with a gratuitous port number like example.com:443. Is this, too, one of those things that is leveled-out anyway in shared hosting?
2:32 am on Aug 24, 2019 (gmt 0)

Senior Member

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2006
posts: 3153
votes: 7


The trailing "$" can also be omitted, in order catch FQDN (that end in a dot).


Oops, sorry, got the logic reversed there(!) ... it's a negated condition, so the "$" should be included - as it was initially - ie. "!^example\.com$" (in order to catch a FQDN that ends in a dot!). Since you want to redirect when the requested host is not exactly "example.com". As you were. :)

Aside:

In order to catch say-what-now? .... Is this, too, one of those things that is leveled-out anyway in shared hosting?


A fully-qualified-domain-name strictly ends in a dot and no, not usually.
3:02 am on Aug 24, 2019 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:15934
votes: 889


FQDN
:: detour to abbreviations dot com ::

Oh. I see. D’oh! Yes, sometimes I have to be told the same thing more than once, or I have to fiddle with it a bit and turn it from side to side until I get it to fit into my brain.

They also claim that in German it stands for Foll Qualifizierter Domizil Name which, er, I tend to doubt.
5:30 am on Aug 24, 2019 (gmt 0)

New User

Top Contributors Of The Month

joined:Aug 7, 2019
posts: 31
votes: 0


Hi not2easy,

"What is that (20) that Lyxx shows at "Line 6"?"

I don't know. There is no annotation, no documentation on that tool on their website, or any documentation of any of the 3 htaccess testing tools I could find. I wish I did know.

"Also are you sure that
!^(example\.com)$?

is what you want?

!^(example\.com)?$

is more common and may be why the tool is gagging?"

I don't know if I want that, I'm just using the Apache code others have suggested in this thread. I'm no developer, but I can do some basic troubleshooting and modification of html and a bit off CSS. But I don't know Apache.

I tried changing the order like you suggested but got the same error

Lyxx found 1 problem in the code segment below.

line 4: # Redirect non-HTTP or non-www to HTTPS without www
line 5: (20) RewriteCond %{HTTPS} off [OR]
line 6: RewriteCond %{HTTP_HOST} !^(example\.com)$?
5:49 am on Aug 24, 2019 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:15934
votes: 889


!^(example\.com)$?
This is wrong, and may even result in a 503 error if you try it on a live site. Nothing can come before a ^ anchor or after a $ anchor.

:: wandering off to test site ::

:: returning 2 minutes later ::

Yup, that's a server error. Error logs, if anyone wondered, say (with appropriate edits):
[Fri Aug 23 22:50:21.223202 2019] [core:alert] [pid 13221] [client 75.31.aa.bb:51394] /full/filepath/example.com/.htaccess: RewriteCond: cannot compile regular expression '^(www\\.example\\.com)$?'
7:04 am on Aug 24, 2019 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:11872
votes: 245


Also are you sure that
!^(example\.com)$?
is what you want?
!^(example\.com)?$
is more common and may be why the tool is gagging?

thanks for noticing this, which is my fault due to a typo i posted early in the thread and which then persisted in subsequent posts.
i have edited the typo in the earlier posts by myself and endlesscuriosity to avoid future cut-and-paste errors.
the typo remains in the later posts where the code error itself is being discussed or where code validation is being discussed.
hopefully this reduces the overall noise and avoids future confusion.
7:22 am on Aug 24, 2019 (gmt 0)

New User

Top Contributors Of The Month

joined:Aug 7, 2019
posts: 31
votes: 0


So now I'm even more confused.

The htaccess tester that seems to have the most feedback is at madewithlove DOT be (put the dot so not to break the rules on pasting urls here) is giving even more errors and not even outputting the right url

Where it says "The request URL (always starts with an http:// ) I put the old [www...] URL
http:/www.example.com/addintheworkplacearticles.html
that I want to redirect to a new https:// non www file
https://example.com/manage-adhd-at-work/

Then I paste this code that you seem to be suggesting below

# 301 Redirect 1
RewriteRule ^addintheworkplacearticles\.html$ https://example.com/manage-adhd-at-work/ [R=301,L]

# Redirect non-HTTP or non-www to HTTPS without www
RewriteCond %{HTTPS} =off [OR]
RewriteCond %{HTTP_HOST} ^(example\.com)?$
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]

But the results are worse and it doesn't redirect it to the new url I want.

1 RewriteRule ^addintheworkplacearticles\.html$ https://example.com/manage-adhd-at-work/ [R=301,L]

This rule was not met.

2 RewriteCond %{HTTPS} =off [OR]

This condition was not met but the [OR] option made it pass.

3 RewriteCond %{HTTP_HOST} ^(example\.com)?$

This rule was not met.

4 RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]

This rule was not met.

I'm not sure what to do.
7:44 am on Aug 24, 2019 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:11872
votes: 245


1 RewriteRule ^addintheworkplacearticles\.html$ https://example.com/manage-adhd-at-work/ [R=301,L]

This rule was not met.

this rule should have fired so there is either a typo in the rule or the request or the validator is in error.

3 RewriteCond %{HTTP_HOST} ^(example\.com)?$

This rule was not met.

this should be:
3 RewriteCond %{HTTP_HOST} !^(example\.com)?$
9:30 am on Aug 24, 2019 (gmt 0)

Senior Member

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2006
posts: 3153
votes: 7


Where it says "The request URL (always starts with an http:// ) I put the old [www...] URL
http:/www.example.com/addintheworkplacearticles.html


I assumed this was just how you had typed the URL in your post (to avoid auto-linking?), but if you really did only have 1 slash after the protocol (ie. "http:/www") then you will indeed get the "incorrect" results you are seeing.

I tried changing the order like you suggested but got the same error
Lyxx found 1 problem in the code segment below.


I would avoid using the Lyxx tool. As mentioned in my post above, it is returning spurious errors where there are no errors (and doesn't actually state what the error is) and is not catching the real errors.

The htaccess tester that seems to have the most feedback is at madewithlove


The madewithlove tool tests the logic. The other two tools are "simply" syntax checkers.
10:02 am on Aug 24, 2019 (gmt 0)

New User

Top Contributors Of The Month

joined:Aug 7, 2019
posts: 31
votes: 0


D'oh! All that focus on the apache code missed that // html:) Thanks penders. Also thanks for clarifying about the logic vs syntax. That fix got The Output url to be what I want namely this.

https://example.com/manage-adhd-at-work/

It also got this Rewrite Rule to work.

RewriteRule ^addintheworkplacearticles\.html$ https://example.com/manage-adhd-at-work/ [R=301]The new url is https://example.com/manage-adhd-at-work/

Test are stopped, a redirect will be made with status code 301

But all the other code, each line said "This rule was not met."
10:08 am on Aug 24, 2019 (gmt 0)

New User

Top Contributors Of The Month

joined:Aug 7, 2019
posts: 31
votes: 0


Thanks for clarifying and fixing that phranque.

Lucy24 I do have shared hosting.

So Lucy24 you say for this code RewriteCond %{HTTP_HOST} !^example\.com

"
!^(example\.com)$?
This is wrong, and may even result in a 503 error if you try it on a live site. Nothing can come before a ^ anchor or after a $ anchor."

But then pharnque says I should put the ! in front of the ^(example\.com)?$

"this should be:
3 RewriteCond %{HTTP_HOST} !^(example\.com)?$
"
Which is correct?
10:20 am on Aug 24, 2019 (gmt 0)

New User

Top Contributors Of The Month

joined:Aug 7, 2019
posts: 31
votes: 0


Again I'm using shared hosting.
When I did this

RewriteCond %{HTTP_HOST} !^example\.com

Or this

RewriteCond %{HTTP_HOST} !^(example\.com)?$

Both got “This Rule was not met” by madewithlove

If I took away the !

Both this

RewriteCond %{HTTP_HOST} ^example\.com

And this

RewriteCond %{HTTP_HOST} !^(example\.com)?$

Both got “This Rule was not met” by madewithlove

Also If I did this

RewriteCond %{HTTPS} off [OR]

Or this

RewriteCond %{HTTPS} =off [OR]

Both got “This Rule was not met” by madewithlove

All the the combos of the two RewriteCond and RewriteRule I tried also got “This Rule was not met” by madewithlove
10:21 am on Aug 24, 2019 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:11872
votes: 245


!^(example\.com)$?
This is wrong, and may even result in a 503 error if you try it on a live site. Nothing can come before a ^ anchor or after a $ anchor."

But then pharnque says I should put the ! in front of the ^(example\.com)?$

"this should be:
3 RewriteCond %{HTTP_HOST} !^(example\.com)?$
"
Which is correct?

lucy24's statement is correct regarding regular expressions.
in this case the regular expression is:
^(example\.com)?$

however in the syntax of the RewriteCond directive you can negate the regular expression, so this is correct:
RewriteCond %{HTTP_HOST} !^(example\.com)?$
10:29 am on Aug 24, 2019 (gmt 0)

New User

Top Contributors Of The Month

joined:Aug 7, 2019
posts: 31
votes: 0


Pender, thanks for clarifying, I didn't know. But I got rid of the L flag changing the page redirects from this

# 301 Redirect 1
RewriteRule ^addintheworkplacearticles\.html$ https://example.com/manage-adhd-at-work/ [R=301,L]

to this

To # 301 Redirect 1
RewriteRule ^addintheworkplacearticles\.html$ https://example.com/manage-adhd-at-work/ [R=301]

and adding the other two redirects to test

And changing the last line from this

RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]

to this

RewriteRule ^(.*)$ https://example.com/$1 [R=301]

and pasted in this.

# 301 Redirect 1
RewriteRule ^addintheworkplacearticles\.html$ https://example.com/manage-adhd-at-work/ [R=301]

# 301 Redirect 2
RewriteRule ^adhdmedicationart\.html$ https://example.com/adhd-medication-articles/ [R=301]

# 301 Redirect 3
RewriteRule ^adhdmedicationco\.html$ https://example.com/adhd-medication-company-websites/ [R=301]

RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^(example\.com)?$
RewriteRule ^(.*)$ https://example.com/$1 [R=301]

Result? Only got the first one properly redirected, not the other two. All the other code said as usual, This rule was not met.

1 RewriteRule ^addintheworkplacearticles\.html$ https://example.com/manage-adhd-at-work/ [R=301]The new url is https://example.com/manage-adhd-at-work/

Test are stopped, a redirect will be made with status code 301

2 RewriteRule ^adhdmedicationart\.html$ https://example.com/adhd-medication-articles/ [R=301]

This rule was not met.

3 RewriteRule ^adhdmedicationco\.html$ https://example.com/adhd-medication-company-websites/ [R=301]

This rule was not met.

4 RewriteCond %{HTTPS} off [OR]

This rule was not met.
5 RewriteCond %{HTTP_HOST} ^(example\.com)?$

This rule was not met.

6 RewriteRule ^(.*)$ https://example.com/$1 [R=301]

This rule was not met.
11:01 am on Aug 24, 2019 (gmt 0)

New User

Top Contributors Of The Month

joined:Aug 7, 2019
posts: 31
votes: 0


Thanks phranque. But that line of code still showed "This rule was not met."

I posted this.

http://www.example.com/addintheworkplacearticles.html

then

RewriteRule ^addintheworkplacearticles\.html$ https://example.com/manage-adhd-at-work/ [R=301]

RewriteCond %{HTTPS} =off [OR]
RewriteCond %{HTTP_HOST} !^(example\.com)?$
RewriteRule ^(.*)$ https://example.com/$1 [R=301]

Results

Debugging info

1 RewriteRule ^addintheworkplacearticles\.html$ https://example.com/manage-adhd-at-work/ [R=301]

The new url is https://example.com/manage-adhd-at-work/
Test are stopped, a redirect will be made with status code 301

2 RewriteCond %{HTTPS} off [OR]

This rule was not met.

3 RewriteCond %{HTTP_HOST} !^(example\.com)?$

This rule was not met.

4 RewriteRule ^(.*)$ https://example.com/$1 [R=301]

This rule was not met.
11:04 am on Aug 24, 2019 (gmt 0)

Senior Member

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2006
posts: 3153
votes: 7


This rule was not met.


But that is correct. (That is not an error.) The rule was not met, because the previous rule dealt with it... done... finished. On a real server, those "rule was not met" directives would simply not be executed.

You need to change the URL you are requesting in order to test the other directives. (It looks like you are perhaps trying to test everything in one go? You need multiple tests to test multiple/different rules.)

If the request is caught by any of the initial redirects then your last rule has nothing to do, since you have already canonicalised the hostname ("example.com" and not "www.example.com") and protocol ("HTTPS", not "HTTP").

And you should have the "L" flag on these redirects - as you did initially. Otherwise, at best, processing continues unnecessary through the file. At worst, another directive rewrites the URL a second time and you end up with a conflict / malformed redirect.

And you need to negate that condition.... by prefixing with a "!", as phranque mentioned in the previous post. (EDIT: Which I see you've done in your last post.)
1:19 pm on Aug 24, 2019 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:11872
votes: 245


And you should have the "L" flag on these redirects - as you did initially.

agreed.
Pender, thanks for clarifying, I didn't know. But I got rid of the L flag changing the page redirects...

not sure where you got this idea...
5:42 am on Aug 25, 2019 (gmt 0)

New User

Top Contributors Of The Month

joined:Aug 7, 2019
posts: 31
votes: 0


Thanks for explaining penders. So it did work. Great.

Interestingly I tried reversing the order, just to see what would happen. I know I'm supposed to paste the individual redirects first than the RewriteCond's and RewriteRule last, but when I did switch the order and posted this.

RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^(example\.com)?$
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]

RewriteRule ^adhdmedicationart\.html$ https://example.com/adhd-medication-articles/ [R=301,L]

I got this result. It got the wrong URL output, the old html one vs the new https://example.com one I wanted, but showed the two RewriteCond code lines worked,

But the last RewriteRule added the https but failed to use the new code ending, just the old html post domain keyword.

So that seems to tell me that RewriteCond code should be changed to output not what it's doing now, namely the old url with an https
https://example.com/adhdmedicationart.html

but to output the new URL I want, this.
https://example.com/adhd-medication-articles/

Do you know how to do this?

Output url

https://example.com/adhdmedicationart.html

Debugging info

1 RewriteCond %{HTTPS} off [OR]This condition was met.

2 RewriteCond %{HTTP_HOST} !^(example\.com)?$This condition was met.

3 RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]The new url is https://example.com/adhdmedicationart.html
Test are stopped, a redirect will be made with status code 301

4RewriteRule ^adhdmedicationart\.html$ https://example.com/adhd-medication-articles/ [R=301,L] This rule was not met.
5:46 am on Aug 25, 2019 (gmt 0)

New User

Top Contributors Of The Month

joined:Aug 7, 2019
posts: 31
votes: 0


Hi Phranque,

"not sure where you got this idea..."

I got the “L” flag suggestion from an earlier post from penders

“You don't actually state the URL you are requesting, but if you are requesting "/addintheworkplacearticles.html" (as suggested by the output) then that is correct - only the first directive will be "tested" and processing stops (because of the "L" flag)."

He clarified in his most recent post that I do need it.

"And you should have the "L" flag on these redirects - as you did initially. Otherwise, at best, processing continues unnecessary through the file. At worst, another directive rewrites the URL a second time and you end up with a conflict / malformed redirect. "
10:37 am on Aug 25, 2019 (gmt 0)

Senior Member

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2006
posts: 3153
votes: 7


Interestingly I tried reversing the order


These (mod_rewrite) directives execute in the order they appear in the file. Reversing their order doesn't necessarily make sense. However, in this case, it just makes it less efficient - it will still work....

I got this result. It got the wrong URL output...


Assuming you requested "http://www.example.com/addintheworkplacearticles.html" (HTTP protocol).... what you don't see with that tool is that there will be a second redirect (2nd HTTP request) that triggers the redirect that you require. (It works, just less efficient - 2 redirects instead of 1)

However, if you had requested "https://www.example.com/addintheworkplacearticles.html" (HTTPS protocol) then you would have seen the desired redirect using that tool, because the first rule would not have been met and there would have only been 1 redirect.

So that seems to tell me that RewriteCond code should be changed ...


No it shouldn't. Just reverse the directives - back to how they were. These directives are intended to work together as one complete unit. They each have a specific task. The rule block (with conditions - RewriteCond directives) is the catch-all rule and should appear at the end in this case.


I got the “L” flag suggestion from an earlier post from penders

“You don't actually state the URL you are requesting, but if you are requesting "/addintheworkplacearticles.html" (as suggested by the output) then that is correct - only the first directive will be "tested" and processing stops (because of the "L" flag)."


That was in no way suggesting you should remove the "L" flag (as you are probably now aware). I was simply stating what it was doing. This is the desired behaviour.
This 78 message thread spans 3 pages: 78