Mobile app version of vmapp.org
Login or Join
Kaufman445

: Efficient use of 301 for large redirect map EDIT: As requested below, this is the entire ruleset for one product (obsolete now, as I should be able to consolidate these into ONE simple rule

@Kaufman445

Posted in: #301Redirect #Htaccess #Migration #ModRewrite #Redirects

EDIT: As requested below, this is the entire ruleset for one product (obsolete now, as I should be able to consolidate these into ONE simple rule that looks for the productID#):

RewriteRule /mens-shirts-77/alice-in-wonderland-shirt-662.html$ www.domain.com/alice-in-wonderland-shirt-p160c3 [R=301]
RewriteRule /womens-shirts-92/womens-alice-in-wonderland-shirt-872.html$ www.domain.com/alice-in-wonderland-shirt-p160c3 [R=301]
RewriteRule /index.php?main_page=product_info*&products_id=(662|872)*$ www.domain.com/alice-in-wonderland-shirt-p160c3 [R=301]


And here's the original query string from above that I'm trying to wildcard down to just the productID:

/index.php?main_page=product_info&cPath=77&products_id=694&hipid=63ad73f32f5159a3d0f71b7b92404591




I would like to consolidate the three line rulesets above into a single line (the productID is always attached to any form of the URL, it's the ONLY thing I need to reference from any incoming URL)... this is my first shot, which I believe now is not going to be that easy, but you get the idea:

RewriteRule *(662|872)*$ www.domain.com/alice-in-wonderland-shirt-p160c3 [R=301,L]


From what I understand now, I have to write out a regex that literally takes into consideration every single potential character in the URL's... but with that kind of overkill comes more potential for conflict... for example this rule works for the query string URL, but not the other two:

RewriteRule ^/[A-Za-z.?_=&]+[0-9]+[A-Za-z.?_=&]+(662|872)[A-Za-z.?_=&]+[A-Za-z0-9._%+-]+ www.domain.com/alice-in-wonderland-shirt-p160c3 [R=301,L]


So is it even possible to build a single regex that will handle BOTH forms of URL's (query string AND rewritten) in one formula?

I'm familiar enough with regex to make something, but I'm not familiar enough to understand the right and wrong way to do things... this is for a long history SEO migration to a new platform... I have to get this expression right, and ideally consolidated down to it's most minimal form (there's 300 URL's with multiple incoming versions that need to be accounted for, as shown above)... I don't think I should have 1000 htaccess rules on an already graphic heavy ecommerce site. :/

Thanks for taking a look... I really need to get this right the first time!

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Kaufman445

1 Comments

Sorted by latest first Latest Oldest Best

 

@Ann8826881

I see strange regex out there that appear to be more complex than necessary... for example, why use (.*) instead of just a *???


Because in regular expressions (as opposed to globbing patterns / standard wildcards) the * (asterisk) by itself does not mean anything. The * repeats the previous element 0 or more times. For example, the regex A* will match either nothing, "A", "AA" or "AAA", etc.

In your example *(662|872)*$, the first * is meaningless, it might even result in an error! The second * then repeats the previous element ("662" or "872") zero or more times. So, it will match "" (an empty string), "662", "872", "662662" or "662872", etc.

In fact, the regex you require is probably a lot simpler (not complex). For instance, (662|882) will match "662" or "882" anywhere in the string.

However, if you need to match both query strings and URL paths then this will add complexity as you can't do this with a single RewriteRule. You can't match the query string with the RewriteRule directive. You would need to do something like the following:

RewriteCond %{QUERY_STRING} products_id=(662|872) [OR]
RewriteCond %{REQUEST_URI} -(662|872).
RewriteRule ^ /alice-in-wonderland-shirt-p160c3 [R=301,L]


This assumes you are redirecting to the same domain, if not then you will obviously need the absolute URL in the RewriteRule substitution.


Is it even possible to build a single regex that will handle BOTH forms of URL's (query string AND rewritten) in one formula?


A single mod_rewrite directive - no.

However, do you really need to match the query string and the URL path? You state that one is "rewritten". Are users accessing the site with both URLs? If the URL has already been canonicalised then this could indeed be simplified, as you can simply ignore the query string version. Like so:

RewriteRule -(662|872). /alice-in-wonderland-shirt-p160c3 [R=301,L]


(The additional bits to the regex are just to ensure it doesn't match anywhere else in the URL. If you are sure "662" or "872" cannot appear anywhere else in the URL or part of another product id, then they can be removed.)

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme