301 redirect throws an error saying the page being redirected does not exist - .htaccess

i want to redirect from a page at /roofing/bellevue/index.php to /bellevue-roofing.php I entered the following:
Redirect 301 /roofing/bellevue/index.php http://www.emeraldstate.com/bellevue-roofing.php
into .htaccess in the root directory.
The result of entering www.emeraldstate.com/roofing/bellevue/index.php is:
The requested URL /roofing/bellevue/index.php was not found on this server.
I have checked and rechecked various sources on formatting Redirects and everything seems correct. Can anyone provide a little guidance?

The correct implementation is to avoid specifying the domain name as part of the redirect. Whilst this will more than likely make no difference to your setup, I'm mentioning it anyway.
You'll be better off using RedirectMatch or mod_rewrite (which I prefer):
RedirectMatch 301 ^/roofing/bellevue/index.php$ /bellevue-roofing.php
Or use mod_rewrite (make sure that the extension is enabled, which it generally is):
RewriteEngine On
RewriteRule ^/roofing/bellevue/index.php$ /bellevue-roofing.php [R=301,L]

Related

301 Redirect with cet= parameter

I would like to make a 301 redirect of a url in http://website.com/example-old/?cet=3132 format to a page https://www.website.com/example-new/.
I have tried several times via .htaccess with the classic method:
redirect 301 /example-old/?cet=3132 https://www.website.com/example-new/
The redirect works, but I don't get what I want. In fact the final url becomes https://www.website.com/example-new/?cet=3132.
In short, the ?cet=3132 doesn't disappear and this is not good for me.
I found this invaluable resource which recommends a mod_rewrite method: 301 redirect for old urls with language parameter
Sure I'll try it, but I was wondering: will it work even with the cet=3132 parameter?
Thanks to those who can answer, best regards.
Sure you can use the rewriting module for that:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^cet=3132$
RewriteRule ^/?example-old/?$ https://www.example.com/example-new/ [QSD,L,R=301]
The QSD flag will take care to remove the query string during the redirection.
It is a good idea to start out with a R=302 temporary redirection and to only change that to a R=301 permanent redirection once everything works as intended. That prevents nasty caching issues.

HTACCESS 301 redirect keep sending to the wrong page

I am trying to redirect an old page from a website I have redesigned, to the new one, but it's not working.
Here's my 2 lines of code in the .htaccess file regarding that domain:
Redirect 301 /deaneco http://solutionsgtr.ca/fr/deaneco/accueil.html
RewriteRule ^/deaneco/contact http://solutionsgtr.ca/fr/deaneco/contact.html [R=301,L,QSA]
If go on the solutionsgtr.ca/deaneco/contact URL, it gives me the following page:
http://solutionsgtr.ca/fr/deaneco/accueil.html/contact
The first rule works though (deaneco/ to solutionsgtr.ca/fr/deaneco/accueil.html).
I feel like both lines are being mixed together and are giving me the wrong page, that doesn't exist so I get a 404 error.
There are a couple of issues here:
The Redirect directive (part of mod_alias) is prefix-matching and everything after the match is appended on the end of the target URL. This explains the redirect you are seeing.
The RewriteRule (mod_rewrite) pattern ^/deaneco/contact will never match in a .htaccess context since the URL-path that is matched does not start with a slash. So, this rule is not doing anything currently.
You should avoid mixing redirects from both modules since they execute independently and at different times during the request (mod_rewrite executes first, despite the apparent order of the directives).
Either use mod_alias, ordering the directives most specific first:
Redirect 301 /deaneco/contact http://solutionsgtr.ca/fr/deaneco/contact.html
Redirect 301 /deaneco http://solutionsgtr.ca/fr/deaneco/accueil.html
NB: You will need to clear your browser cache, since the erroneous 301 (permanent) redirect will have been cached by the browser. Test with 302 (temporary) redirects to avoid potential caching issues.
OR, if you are already using mod_rewrite for other redirects/rewrites then consider using mod_rewrite instead (to avoid potential conflicts as mentioned above):
RewriteEngine On
RewriteRule ^deaneco/contact$ http://solutionsgtr.ca/fr/deaneco/contact.html [R=301,L]
RewriteRule ^deaneco$ http://solutionsgtr.ca/fr/deaneco/accueil.html [R=301,L]
The QSA flag is not required, since the query string is passed through to the substitution by default.
The order of the RewriteRule directives are not important in this instance, since they match just that specific URL.
If go on the solutionsgtr.ca/deaneco/contact URL
If you are redirecting to the same host then you don't need to explicitly include the scheme + hostname in the target URL, since this will default.

Why does this redirect apply?

I have a simple Redirect in my htaccess file:
Redirect 301 /foobar /johndoe/foobar
unfortunately, this url:
/foobar/barfoo also gets redirected - why? In my understanding, only /foobar should be redirected when using the Redirect command, shouldnt it?
I feel that this is more comfy than writing RewriteRules
As it turns out, Redirect seems to just check if the URL to check is in the beginning of the current path (at least my tests say that, the documentation is not 100% clear about it).
But, to avoid using RewriteRule (since it might be overkill), simple RedirectMatch also works:
RedirectMatch 301 "^/foobar$" "/johndoe/foobar"
I would still be thankful for additional advice, whether this isnt possible to solve without "regex" and/or RewriteRule

URL rewrites issues

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.

redirect ALL urls from old site to index.php on new one with .htaccess?

A couple questions, simply:
Is the .htaccess file generally stored in the public_html directory? I think so, no?
If I do not find such file, can I simply create it and upload it with FTP?
3 (most importantly). What is the code I need to redirect ALL URLS to ONE new URL, namely, http://www.newsite.com
Is it nothing more than Redirect 301 / http://www.newsite.com/?
Thanks
Is it nothing more than Redirect 301 / http://www.newsite.com/?
Correct, it is the only thing you need. It will redirect anything starting with / to the appropriate place in http://www.newsite.com/. Example:
You go to http://oldsite.com/some/path/to/file.php, you'll get redirected to http://www.newsite.com/some/path/to/file.php.
If you want everything to go simply to the document root of the new site, you can use a RedirectMatch instead:
RedirectMatch 301 .* http://www.newsite.com/
So if you go to http://oldsite.com/some/path/to/file.php, you'll get redirected to http://www.newsite.com/
Not all apache installations come with mod_rewrite installed, if it's not installed you'll get a 500 server error if you attempt to use the rewrite engine. However, mod_alias is usually always installed.
Question 1: Whether you can just upload .htaccess to public_html depends on the web host, but that will likely work.
Question 2: I would set up the redirect like this:
RewriteEngine On
RewriteRule ^(.*) http://www.newsite.com/ [R=301,L]

Resources