.htaccess redirect partial url pattern - .htaccess

I am having issues with writing the proper redirect statement, and unfortunately can't rap my head around the syntax needed. Other questions have similar problems, but I can't figure out how to properly reuse the information in other posts.
I have a url: www.site.com/.../CORE_Testing_5010 this unique.
I need the page to redirect to www.site.com/core-phase
The ... could be multiple directories /a/b/c/d/CORE_Testing_5010 or just /a/CORE_Testing_5010
Right now, I have 310 redirects for most of the possible directory combinations, but that is inefficient.
Some guiedence and explanation would be helpful.

Try:
RedirectMatch 301 /CORE_Testing_5010$ /core-phase
The regex pattern ignored everything before the last /, so there can be anything in front of /CORE_Testing_5010.
If you have rewrite rules, and using mod_alias (the RedirectMatch) is causing conflicts, then you can stick with mod_rewrite:
RewriteRule /?CORE_Testing_5010$ /core-phase [L,R=301]
and it needs to be before any rules that do routing.

Related

Changing Domain: 301 Redirect for multi language E-Commerce site

I am moving my WP ecommerce site to a new domain and I need to code a more advanced htaccess 301 redirect to pass on the SEO love. (I say htaccess but maybe there is a server side way that is better)
I have made sure, as much as possible, to keep the URL structure the same so for the products/posts, categories, tags and most pages everything after the olddomain.com/XXXXX is the same.
However, I don't want to do a blanket redirect for everything because there are some parts of the site that will not match so I thought it better to break into into chunks/functions.
(Maybe this is a bad strategy and I should just do do one blanket redirect and the trouble shoot page not found as it all goes live?)
redirect function for products
redirect function for categories
redirect function for tags
individual redirects for the rest
There are also three languages with sub folder /ca/ and /es/ - example.com/es/products - assuming I can just copy the function for each language and appending the subfolder.
Examples:
oldomain.com/product/any-product-ABC
redirect to
newdomain.com/product/any-product-ABC
(Domain change) (folder same) (product added from previous)
Then same redirect for languages
oldomain.com/es/product/any-product-ABC
redirect to
newomain.com/es/product/any-product-ABC
How do I write the above redirects?
I say htaccess but maybe there is a server side way that is better
If you have access to the Apache server config then you can indeed simplify these redirects, which will also be more efficient since it will reduce the load (if any) from the main site.
Create a separate <VirtualHost> container for the old domain and then you can use simpler mod_alias directives.
For example, for the two examples you gave. eg. /product/<product-name> or /<lang-code>/product/<product-name> to the same URL at the new domain then you can use following single rule:
RedirectMatch 301 ^(/[a-z]{2})?/product/[\w-]+$ https://newdomain.com/$0
This matches any optional 2-character language code. If you only have three languages then you could be more specific and use regex alternation instead, eg. (ca|es|id) in place of [a-z]{2}. The <product-name> is assumed to consist of the following characters only: a-z, A-Z, 0-9, _ (underscore) and - (hyphen).
However, if you are restricted to .htaccess and both domains resolve to the same place then you will need to use mod_rewrite and check the requested hostname. For example, the following would need to go near the top of the root .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^olddomain\.com [NC]
RewriteRule ^((ca|es|id)/)?product/[\w-]+$ https://newdomain.com/$0 [R=301,L]
Note the regex is slightly different to the mod_alias RedirectMatch directive used above since the RewriteRule pattern (first argument) matches against the URL-path less the slash prefix.
Do not repeat the RewriteEngine directive if it already occurs elsewhere in the config file. The order of the directives is important. The above redirect must go before any existing internal rewrites and ideally before any canonical redirects (to minimise the number of redirects).
You should first test with 302 (temporary) redirects to avoid potential caching issues.
so I thought it better to break into into chunks/functions.
That's fair enough. Although you need to make sure that any URLs that you don't redirect from the old domain return a 404 or 410.
And it may be possible to combine "products", "categories" and "tags" into a single rule, depending on exactly the format of these URLs.

How to redirect only when there is something after .html?

I have found that there are some people with bad syntax links to our articles.
For example, we have an article with URL
http://www.oursite.com/demo/article-179.html
The issue is that lot of people have linked back to this article with bad syntax such as
http://www.oursite.com/demo/article-179.html%5Cohttp:/www.oursite.com/demo/glossary.php
Now, I added the following ReWrite Rule in the .htaccess file to take care of such links.
RewriteRule article-179\.html(.*)$ "http\:\/\/www\.oursite\.com\/demo\/article-179\.html [301,L]
But this has resulted in a Redirect Loop message. How can we fix this issue via htaccess rewrite rule. Basically, we need something in our rewrite rule that works only when there is one or more characters after the .html. If not, then it should not redirect.
Any help would be highly appreciated!
With best regards!
Use + instead of *. * matches zero or more, which causes the pattern to match for the redirected path too, + instead matches one or more.
Also you should make the pattern as precise as possible, ie don't just check whether it ends with article-179.html, better check for the full path. And if this all happens on the same domain, then there's no need to use the absolute URL for the redirect.
There's also no need for escaping the substitution parameter like you did, it's treated as a simple string except for:
back-references ($N) to the RewriteRule pattern
back-references (%N) to the last matched RewriteCond pattern
server-variables as in rule condition test-strings (%{VARNAME})
mapping-function calls (${mapname:key|default})
http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewriterule
Long story short, theoretically this should do it:
RewriteRule ^demo/article-179\.html(.+)$ /demo/article-179.html [R=301,L]
or this if you really need the absolute URL:
RewriteRule ^demo/article-179\.html(.+)$ http://www.oursite.com/demo/article-179.html [R=301,L]

htaccess rewrite regex for pagination urls

I need an expression that would rewrite (actually 301 redirect) old url patterns ended with -0.htm, to new ones -1.htm
so if old url was /path/to/pagination/script-0.htm
new url must be /path/to/pagination/script-1.htm
This must work for any number of folder/subfolder structure.
And must work only for [-0.htm]. Only for urls that end with this pattern.
Any idea on how to accomplsh this?
My htaccess regex knowledge is limited to none.
You could try something like:
RewriteRule ^(.*?)\-0\.htm$ $1-1.htm [R=301,L]
But make sure it's one of the first rules, before the ones that might do a pass-through if a real file.

Why htaccess redirect being overruled?

I've got:
Redirect 301 /blog/?p=1 http://www.new-site.com/blog/2000/10/myslug/
which works fine, unless followed by:
RedirectMatch 301 ^/blog(/)?(.*)$ http://www.new-site.com/blog/$2
I've tried all kinds of versions, including RewriteRule, but nothing has worked. How do I keep the first specific rule, and write an "everything else keeps its request uri and query string" rule?
Thanks
Alright, assuming these are the only two lines, what I see is this:
Redirect 301 /blog/?p=1 http://www.new-site.com/blog/2000/10/myslug/
RedirectMatch 301 ^/blog(/)?(.*)$ http://www.new-site.com/blog/$2
These are basically saying the same thing, that is, on a match, permanently redirect all blog queries to the new site.
With the second one you're saying match from the beginning the string /blog with a possible slash, which you'll capture, and possibly more information, which you'll also capture, then just put all that information into blog/extra-picked-up-info. This may be part of the problem, or you may be able to get around it by reordering the directives, and seeing if the lower directive receives precedence.
RedirectMatch 301 /blog(?:/\?)?(.*)?$ http://www.new-site.com/blog/$1
Redirect 301 /blog/?p=1 http://www.new-site.com/blog/2000/10/myslug/
Otherwise, you're going to need to reexamine your URIs, and find something more uniquely identifying.

Redirecting an already redirected page

RewriteRule ^teamstore/(.*)/$ /teamproduct.php?teamproduct=$1&products=true [NC,L]
RewriteRule ^teamstore-(.*)/$ /teamproduct.php?teamproduct=$1&products=true [NC,L]
Here's the situation. We've already had theese urls rewritten, but we want to change the formatting of these pages so they're separated by hyphen instead of a slash.
I tried redirectmatch but this added additional php value parameters to the end of the urls. It came out to be
RedirectMatch 301 /teamstore/(.*) http://www.domainname.com/teamstore-$1/
Here was the result...
teamstore-valuehere//?teamproduct=2352323&productes=true
I want someone who types in the original address teamstore/info/ to get directed to teamstore-info/ - Any ideas on how to accomplish this?
The main reason is to avoid duplicate content issues with existing links in Google Search Results.
Order of the rules can matter, but if you want to 301 redirect the initial request for SEO reasons, then I would change your RewiteRules to the following.
RewriteRule ^teamstore/(.*)/$ teamstore-$1/ [R=301,L]
RewriteRule ^teamstore-(.*)/$ /teamproduct.php?teamproduct=$1&products=true [NC,L]
This will first translate teamstore/info/ to teamstore-info/ and send the appropriate 301 response. Upon second pass it will redirect to the php you want.
Note, this is not ideal as far as performance. Yet, it does accomplish the goal of making Google happy.

Resources