0

Summary: I've been asked to migrate a .asp site over to Wordpress, but I'm having some issues with the .htaccess redirects.

What I'm trying to Achieve:

Pages:

domain.com/Some-Page-On-My-Site.asp

redirects too

domain.com/some-page-on-my-site/

Posts:

domain.com/articles.asp?title=The-Greatest-Post-Title

redirects too

domain.com/blog/the-greatest-post-title/

End Goal

  • Stay consistent with default Wordpress permalink structure using lowercase.
  • Convert only the .asp URL requests to lowercase not the rest of the site.

I'm thinking that because of the *.asp pages redirect that it may be sucking in the articles.asp and converting it to /articles/ . So I swapped their spot in the .htaccess file.

I'm thinking that RewriteCond and Skip Flag is probably required for this to work. But could be wrong.

I'm glad I was able to get the Pages redirect to work, although I think there is just a little more to make these work better.


What I've tried

This converts URL Requests for old .asp pages to new URL Structure:

RewriteRule ^(.*)\.asp$ /$1/? [L,R=301]

These attempts do not convert URL Requests for old .asp posts:

# Try 1
RewriteRule ^articles\.asp?title=(.+)$ /blog/$1/ [L,R=301]

# Try 2
RewriteRule ^articles\.asp?title=(.*)$ /blog/$1/ [L,R=301]

# Try 3 - Not that this would probably work but was an attempt
RewriteCond %{QUERY_STRING} (^|&)title\=(.+)($|&)
RewriteRule ^articles\.asp$ /blog/$1/? [L,R=301]

I confirmed that I can do 1-to-1 redirects, but not similar to the Pages structure like above where I can type anything before .asp and it just removes the .asp

RewriteCond %{QUERY_STRING} (^|&)title\=Duck\-Feet\-Wine($|&)
RewriteRule ^articles\.asp$ /blog/duck\-feet\-wine/? [L,R=301]

Does anyone have some insight on this? or am I crazy to think that I can do this?

I've read about 100+ pages and I'm stumped, including stumped on how RewriteCond works. I happy that I was able to get the pages redirect to work, now to convert a query string to a static like link to work with Wordpress's lowercase permalink structure.

Arnaud A.
  • 13
  • 3
  • I’ve read over some more docs and tutorials and I’m stumped. I’ve been able to try a few methods that worked, but they are 1 for 1 links. Domain,com/articles.asp?title=post-one. >> Links too>> domain,com/blog/post-one/ But unfortunately I haven’t figured out how to wildcard the query link and redirect to the new url structure. It seems like it should be easy but I do know that it’s also not the easiest to work with at times just for this reason. I would like to make this as dynamic as possible so the site doesn’t have 500 lines of redirect code. Not fun. – Arnaud A. Sep 02 '19 at 23:02
  • RewriteRule matches against the path component of the requested URL only - if you need to check query string contents, you need to do so using a RewriteCond before your RewriteRule. – misorude Sep 03 '19 at 07:52
  • Try the following two lines, `RewriteCond %{QUERY_STRING} ^title=(.*)$` `RewriteRule ^articles\.asp$ /blog/%1/ [L,R=301]` – notice how this uses `%1` here in the substitution, to refer back to the match from the condition. (And place it before your more general rule that rewrites everything that matches `^(.*)\.asp$`, otherwise that will handle `articles.asp?title=...` already.) – misorude Sep 03 '19 at 07:59
  • How do I go about excluding articles.asp from the General all purpose one that I wrote? – Arnaud A. Sep 04 '19 at 03:09
  • By putting them in the right order, that should be enough - always go from more specific to less specific with your rules. The `L` flag should prevent following rules from applying, once the one for the articles has matched. – misorude Sep 04 '19 at 06:26
  • Thank you immensely for that, just got home so trying that now. I'm glad that I was right there and not out in the middle of nowhere. Just needed to use what I was thinking I was going to need to use to get it to work. Do you know a good resource or How To website that explains Mod_Rewrite any better than what I found? – Arnaud A. Sep 04 '19 at 07:07
  • So far I've tested this out it works as intended. My next question and not sure if I included it above, but how do I target both of these rules only to make the output all lowercase? and maybe convert "_" to "-" – Arnaud A. Sep 04 '19 at 07:13
  • Although this is probably not needed as it seems to actually see -> domain.com/A-Page/ and domain.com/a-page/ as the same. I just wanted to make sure that it wouldn't be an issue overall. – Arnaud A. Sep 04 '19 at 07:15
  • Underscores to hyphens - https://stackoverflow.com/q/7650542/10283047, https://stackoverflow.com/q/18673576/10283047 Sorry, overlooked that you wanted to do a case conversion as well. That is not a thing easily done in .htaccess (unless you wanted to extend the principle from the two mentioned questions for every possible character) - it can be done using RewriteMap, but that can only be used in the server configuration, https://httpd.apache.org/docs/2.4/rewrite/rewritemap.html#int – misorude Sep 04 '19 at 07:35
  • _“as it seems to actually see -> domain.com/A-Page/ and domain.com/a-page/ as the same”_ - that is probably due to WordPress itself though, that does a case-insensitive lookup of the post slug in the database. I don’t know if it automatically redirects to the “correctly cased” version; if it doesn’t, then I’d at least make sure the canonical URL meta tag holds the “correct” version, so that search engines don’t give you trouble with duplicate content. – misorude Sep 04 '19 at 07:39
  • I'm not sure I exactly follow your last comment about canonical URLs. It was suggested by someone else on another platform to use a .php file and rewrite the URLs through that, but almost feel this is more resource intensive, and a waste of a file request. – Arnaud A. Sep 04 '19 at 09:19
  • https://en.wikipedia.org/wiki/Canonical_link_element - this tells search engines what they should consider the “real” URL for a certain page, if that page can be reached by multiple different URLs. WordPress usually automatically generates them. – misorude Sep 04 '19 at 09:29

0 Answers0