htaccess partial filename suffix match - .htaccess

As part of a site migration, I have a bunch of old files that I want to redirect to a new site.
The old files all end with _reviews.html, but the new destination is on a different server, without the "_reviews" part of the name, but still ending in .html.
For example:
Old:
www.a.com/my_product1_reviews.html
New:
www.b.com/my_product1.html
I have a few hundred like this so I didn't really want to do them one at a time, but I can't seem to get the correct syntax.

Try this:
RewriteEngine On
RewriteBase /
RewriteRule (.*)_reviews.html$ http://www.b.com/$1.html [L,R=301]

Related

too many redirects. setting up htaccess to redirect

I moved an old website to a new cms and some content have links that cannot be found on the new system.
For instance in the old system there was a link called https://example.com/newsletter/1234/123
in the new one i do not require those ids at the end. Therefore I would like to redirect the user to
https://example.com/newsletter/ directly.
I wrote the following in my htaccess file:
RewriteRule ^(.*newsletter)(.*) $1 [L,R=301]
This gives me unfortnately a "too man redirects" error.
Can anyone point out the error that I made?
I planned to use the regex to capture all links that contain the name "newsletter" and remove the rest of the url to redirect the user. Any help is appreciated thanks.
Using (.*newsletter)(.*) causes the rewrite to match /newsletter alone because .* matches zero or more characters. This sends mod_rewrite into a loop.
Instead, you can use .+ which matches one or more characters, and to that I would prepend /? to optionally match a trailing slash. There is no need to capture it in () because you do not reuse it as $2.
RewriteRule ^(.*newsletter)/?.+ $1 [L,R=301]
Your example began with .*. If you actually need that, leave it in. But if you are really just trying to match /newsletter/123/1234 and not /someother/newsletter/123/1234, simplify it with:
RewriteRule ^newsletter/?.+ /newsletter [L,R=301]
When you test this, use a private/incognito browsing window or a fresh browser. Browsers aggressively cache 301 redirects and it can make it very hard to debug rewrite rules if you are fighting against the cache.

301 Redirect in htaccess. Matching Page IDs

I would like some help creating an htaccess 301 redirect for the below type of url.
In total there's around 500 or so products but rather than write a redirect for every url, which would be very bulky and time consuming, I'm hoping there's an easier way that I haven't yet found to create a kind of regular expression match?
OLD: http://www.example.co.uk/test-product-name-slug/prod_233.html
NEW: http://www.example.co.uk/test-product-name-slug-233.html
The new URL can be accessed by browsing to ..... example.co.uk/-223.html ...... which then rewrites to ..... example.co.uk/test-product-name-slug-233.html
So it would appear I need a way of detecting if the incoming visitor is coming to a url that cotains prod_id and redirecting to -id
I hope that all makes sense.
Hopefully this is what you're looking for. It only matches 1 directory in
/some-product_name-random/prod_9393.html => /some-product_name-random-9393.html
.htaccess
RewriteEngine on
RewriteBase /
RewriteRule ^([a-zA-A-_]+)/prod_([0-9]+)\.html$ /$1-$2.html [R=301,L,QSA]
Regex and Parameters Explained
([a-zA-A-_]+) matches product name $1
prod_([0-9]+) matches product id $2
[R=301] 301 permanent redirect
[L] stop .htaccess script (may be removed, but usually good practice for specific rules when using multiple rules for different scenarios)
[QSA] keep query string domain.com/somepath/page.html?querystring=value&otherstuff (?...)

htaccess help to rename directory

I have 2 directories that I would like to rename without losing my search ranking. Here is the url convention and suggested name change:
Old: mysitename.com/folder1/folder2/filenames
to
New: mysitename.com/newfoldername1/newfoldername2/filenames
filename is the given name of each page
RewriteRule ^(.*)/folder1/folder2/(.*)$ $1/newfoldername1/newfoldername2/$2 [R,L]
The problem with this rule is that RewriteRules only match on the path, not the domain. You have the (.*)/ at the front of the rule as if you are trying to match the domain, which isn't necessary. You have also set it as a redirect rather than a rewrite ... maybe this is what you want, in which case keep the "R" flag as you had it, but I'll remove it in the code below in case you just want to rewrite. Modify the rule as follows and it should work:
RewriteRule ^newfoldername1/newfoldername2/(.*)$ /folder1/folder2/$1
Edit: Now that you've posted your other rules, I removed the [L] and reversed the order to what I think you're trying to do.
Edit 2: The above rule assumed that you wanted a user entering domain.com/newfoldername1/newfoldername2/whatever to be silently rewritten to domain.com/folder1/folder2/whatever. The phrasing of your latest comment indicates that instead you want a user entering domain.com/folder1/folder2/whatever to be redirected (in other words, to have the address change in their browser) to domain.com/newfoldername1/newfoldername2/whatever and that the server is ready to process this new path. In that case, the following rule is required:
RewriteRule ^folder1/folder2(/.*)?$ /newfoldername1/newfoldername2$1 [R=301,L]

htaccess redirect parameter into directory

I setup a slightly weird hierarchy where index reads the URI to decide the action instead of using actual subdirectories, example: site.com/base/action/
Im moving over from the old system to this new one, but I cant get the redirects to work. What I need is: /base/?param[]=value to do an external redirect to /base/param/value/
Try putting this in the htaccess file in your document root:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(.+?)(\[\])?=([^&]+)&?(.*)$
RewriteRule ^/?base/?$ /base/%1/%3/?%4 [L,R=301]
This will also preserve any query string that may be after param[]=value. You can alternatively just clobber the entire query string altogether by removing the %4 at the end of the rule's target.

.htaccess 301 Redirect Appending Query's to End of URL

I am hoping someone can help with an unusual situation.
I have one main rewrite rule in place in my httpd.conf file which handles all of our dynamic content. The rule looks like this and works fine:
RewriteRule ^(.)(/./d/[^.]*)$ /category/refine.cgi\?\&a\=$2
The problem I have is that when I try to use .htaccess to create a simple 301 redirect, the query parameters are automatically appended to the end of the URL's so the final result looks like this:
http://www.example.com/category/page.html?&a=/category/subcategory/something/d/page/
Notice that the query string is appended to the URL when using .htaccess to create a 301 redirect.
I have solution for this on a case-by-case basis, but it's not practical to create a new rule each time I want to do a simple 301 redirect.
So, I am wondering if I can edit my "main rule" in any way so that when .htaccess is used to create redirects, the query parameters are not appended to the target URL.
Thanks in advance for any help you can provide.
If you have multiple simple redirects for which you want to suppress query string values you could put all the redirects in a RewriteMap (since you already have access to httpd.conf), and have one .htaccess rule that suppresses the query strings as below
place in htaccess
#if there is a match in the map
RewriteCond ${redirect_map:$1} !=""
RewriteRule ^(.*)$ ${redirect_map:$1}? [R,L]
place in httpd.conf
RewriteEngine On
RewriteMap redirect_map txt:/usr/local/apache/conf/redirect.map
contents of /usr/local/apache/conf/redirect.map
key followed by a space followed by target
directory/subdirectory1/subdirectory2/ example/category7/subdirectory/file.html
directory4/subdirectory2/subdirectory9/ example/category5/subdirectory4/file332.html
That's what your rule has defined it to do:
RewriteRule ^(.)(/./d/[^.]*)$ /category/refine.cgi\?\&a\=$2
It says to create a URL that will look like:
category/refine.cgi?&a=/foo/bar/
If you don't want that to happen, change your rule to be:
RewriteRule ^(.)(/./d/[^.]*)$ /category/refine.cgi\?

Resources