I would like to add code line in .htaccess to redirect a package of url with the same pattern :
shop.my-example.com/blog/{my-cat}/{number}
To a new site :
https://my-new-site.com/blog/{my-cat}/{number}
For the moment I'm using this code line :
Redirect permanent /blog/ https://www.my-new-site.com/blog/
But it works only fr the url shop.my-example.com/blog/
Using RedirectMatch, You may use this rule as your top rule (before all other rules):
RedirectMatch 301 ^(/blog/.+)$ https://www.my-new-site.com/$1
Make sure to test in a new browser or clear your browser cache before testing this change.
Related
I am trying to implement a very basic redirect for specific pages with htaccess, however the browser adds a GET variable to the new URL after the redirect:
Redirect 301 /branding/ABCDE http://example.com/branding/NEW
New URL in browser:
http://example.com/branding/NEW?slug=ABCDE
How can I correct this so that the ?slug=ABCDE does not show up?
Without seeing your .htaccess file, the best guess is that you have a conflict with other (specifically mod_rewrite) directives in your .htaccess file. There is nothing actually wrong with the Redirect directive itself that you posted.
The Redirect directive is a mod_alias directive and executes after mod_rewrite, despite the apparent order of these directives in your .htaccess file. A URL of the form /branding/ABCDE most probably requires rewriting (using mod_rewrite) for it to be useful by your application. This rewrite occurs before the Redirect, but any query string will be maintained.
It is not recommended to mix both Redirect and RewriteRule directives in the same config file, because of these unexpected conflicts.
Change your redirect to use mod_rewrite instead and make sure this is near the top of your config file. For example:
RewriteEngine On
RewriteRule ^branding/ABCDE$ /branding/NEW [QSD,R=301,L]
You will need to ensure your browser cache is cleared before testing, since the erroneous 301 (permanent) redirect will likely have been cached by your browser. To avoid caching issues, it is advisable to first test with 302 (temporary) redirects.
The QSD flag (Apache 2.4+) will discard any query string that might be present on the initial request.
https://example.com/folder/ needs to direct to https://example.com/folder.html
I added this to the htaccess,
Redirect 301 /folder https://example.com/folder.html
This works if the url is https://example.com/folder
However if the url is https://example.com/folder/ then it redirects to a broken url at https://example.com/folder.html/
How would I fix this so that https://example.com/folder/ redirects to https://example.com/folder.html/ without the trailing slash causing it to break?
Better to use a RedirectMatch rule that supports regex with more powerful matching options:
RedirectMatch 301 ^/folder/?$ /folder.html
Make sure to test it in a new browser or clear browser cache before testing.
I added this code to my .htaccess file in: https://www.revealio.com/agora
Redirect 302 / https://sales.revealio.com/agora?r_done=1
This should redirect it to our subdomain that is in the code. The problem is that it redirects to: https://sales.revealio.com/agoraagora
Why is it adding another agora onto the end of the URL. How can I fix this. I have tried commenting out everything in the .htaccess file located at https://www.revealio.com/agora other than the redirect. I also tried these:
Redirect 302 / https://sales.revealio.com/agora
Redirect 302 / https://sales.revealio.com/agora/
Use RedirectMatch instead of Redirect for precise regex based matching:
RedirectMatch 302 ^ https://sales.revealio.com/agora?r_done=1
Make sure to test from a new browser or clear your browser cache completely.
I need to create a rewrite rule for an htaccess file which will rewrite URL's such as:
http://archive.citylaw.org/bsa/2014/03.24.14/300-13-A.pdf
to
http://archive.citylaw.org/wp-content/uploads/sites/24/bsa/2014/03.24.14/300-13-A.pdf
The rule needs to match any link such as:
http://archive.citylaw.org/bsa/<file-path>
which is a link to a PDF file to the new location
http://archive.citylaw.org/wp-content/uploads/sites/24/bsa/<file-path>
Something like:
RewriteRule ^(bsa\/.*) /wp-content/uploads/sites/24/$1 [R]
The [R] tell apache to send a redirect, so the browser will update its navbar, usually this avoid browser not dealing well with non html files on rewrite.
Updated after Prix comments, I did forgot about .htaccess specificity
Second edit
We are having a problem with URL rewrites on an apache server using .htaccess.
Goal: to have the following URL stripped of its category & subcategory while leaving the generic redirect in place.
Test 1:
Redirect 301 /category/subcategory/product http://www.site.com/product
Redirect works perfectly. A single redirect to the desired page.
Test 2:
RedirectMatch 301 ^/category/subcategory/.*$ http://www.site.com/category/subcategory
Redirect on its own works perfectly for all URLs desired.
The problem is when we have both URLs in a clean .htaccess file, and the redirects are in the proper order (specific first, then general), the general redirect is being used.
Test 3:
Redirect 301 /category/subcategory/product http://www.site.com/product
RedirectMatch 301 ^/category/subcategory/.*$ http://www.site.com/category/subcategory
When we visit www.site.com/category/subcategory/product, the result is www.site.com/category/subcategory/product, That is not the desired result. Instead, we want the URL to be www.site.com/category/subcategory/product,
We have even tried modified the Redirect to:
Redirect 301 /category/subcategory/product http://www.site.com/product [L]
It made no difference.
Please help!
EDIT: Added 3/25/2014
What we are trying to do is provide specific redirects for a group of known products from their old product page to the new product page. We are also trying to add a "catch all" redirect for the remaining unknown products to the category page.
Here is an actual example redirect which works:
Redirect 301 /womens/western-dresses/stetson-cream-empire-waist-ls-western-dress http://www.site.com/stetson-cream-empire-waist-ls-western-dress
If the above redirect is added to the .htaccess file, it works perfectly on its own.
Here is a second example redirect which works:
RedirectMatch 301 ^/womens/western-dresses/.*$ http://www.site.com/womens/western-dresses
The problem is if we have both of the rules together in .htaccess, in the same order as above, the second rule is always triggered. We try to access www.site.com/womens/western-dresses/stetson-cream-empire-waist-ls-western-dress and the result is www.site.com/womens/western-dresses instead of the desired result of www.site.com/stetson-cream-empire-waist-ls-western-dress
For clarity:
if we remove the .htaccess file, the URL 404s
if only the first rule is listed, it triggers perfectly
if only the second rule is listed, the second rule triggers perfectly
if both rules are listed, the second rule triggers.
We have deleted all redirects from the .htaccess file. The only redirects are the below two lines. The issue remains where the first redirect is ignored. We have tried changing the start of the first redirect to ^/womens and ^womens but that change had no effect.
Redirect 301 /womens/western-dresses/stetson-cream-empire-waist-ls-western-dress http://www.site.com/stetson-cream-empire-waist-ls-western-dress
RedirectMatch 301 ^/womens/western-dresses/.*$ http://www.site.com/womens/western-dresses
Your post is a little confusing, so I may be misunderstanding what you are trying to do.
If memory serves, you should not include a leading slash in your pattern when using these directives in a .htaccess file. That usage is reserved for httpd.conf. When these directives are used in a .htaccess file, the leading path components have already been stripped by mod_access. I am guessing this is the cause of your troubles.
For example, this should work (not tested):
Redirect 301 ^category/subcategory/product http://www.site.com/product
RedirectMatch 301 ^category/subcategory/.* http://www.site.com/category/subcategory
As an aside, [L] is mod_rewrite lingo. "Redirect" and "RedirectMatch" are part of mod_access.
EDIT 3/25:
Redirect and RedirectMatch can be fussy when used in .htaccess files, particularly when dealing with non-existent folders and mixed directives. Can I suggest you move directly to mod_rewrite? While it has a steep learning curve, you will never go back once you get the hang of it.
# Assuming you are in a .htaccess under DocumentRoot:
RewriteEngine On
RewriteRule ^category/subcategory/product1\.html$ /product1.html [R=301,L]
RewriteRule ^category/subcategory/product2\.html$ /product2.html [R=301,L]
RewriteRule ^category/subcategory/.* /category/subcategory [R=301,L]
As an aside, this looks like a good candidate for RewriteMap, although you will need to declare the map in your httpd.conf.