Forum Moderators: phranque

Message Too Old, No Replies

htaccess redirect issue

         

jp12

6:33 pm on Feb 23, 2014 (gmt 0)

10+ Year Member



RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^mysite\.com$ [NC]
RewriteRule ^(.*)$ http://www.mysite.com/$1 [R=301,L]

Redirect 301 /redirect/forecast.weather.gov/mapclick.shtml http://www.nws.noaa.gov/view/largemap.php


When you click the link on my website, the following message appears even though this link is on my server, not sure what is wrong?

Not Found

The requested URL /redirect/forecast.weather.gov/mapclick.shtml was not found on this server.

Apache/2.2.22 (Ubuntu) Server at www.mysite.com Port 80

g1smd

7:15 pm on Feb 23, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Various corrections....

RewriteEngine On

RewriteRule ^redirect/forecast\.weather\.gov/mapclick\.shtml http://www.nws.noaa.gov/view/largemap.php [R=301,L]

RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]


Code goes in htaccess file in root of site.

Presume your link points at example.com/redirect/forecast\.weather\.gov/mapclick\.shtml

jp12

8:24 pm on Feb 23, 2014 (gmt 0)

10+ Year Member



Thank you, I will try this. What if i have multiple different links to redirect when each relevant link is clicked on?

lucy24

9:45 pm on Feb 23, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



What if i have multiple different links to redirect when each relevant link is clicked on?

This is obscure. Are you talking about capturing part of a request when only part of it will be different in the target?

Redirect 301 /redirect/forecast.weather.gov/mapclick.shtml http://www.nws.noaa.gov/view/largemap.php


When you click the link on my website, the following message appears even though this link is on my server, not sure what is wrong?

Not Found

The requested URL /redirect/forecast.weather.gov/mapclick.shtml was not found on this server.


This is worrying, because the server should never even have looked for the "forecast.weather.gov" URL. If it was redirected correctly, the only file the server would ever look for is the one ending in "largemap.php". It makes absolutely no difference whether the pattern URL physically exists or not.

jp12

9:51 pm on Feb 23, 2014 (gmt 0)

10+ Year Member



this is the page where the link exists.

http://www.example.com/temps/index.php

upper left corner usa map listed right under 'Your Local Forecast'

[edited by: phranque at 6:48 am (utc) on Feb 24, 2014]
[edit reason] Please Use example.com [webmasterworld.com] [/edit]

lucy24

11:18 pm on Feb 23, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Oh, interesting approach. Visible URL:

http://www.example.com/redirect/forecast.weather.gov/mapclick.shtml

... and then instead of doing some php stuff the site itself redirects on the pattern of

RewriteRule ^redirect/(.+) http://$1 [R=301,L]

:: detour to test site to confirm that you can do that ::

Except you're not actually capturing, are you? You're issuing a redirect to a different page. Why don't you simply change the URL on the first page?


Pause here as I realize the answer was staring me in the face all along. You can't mix mod_rewrite and mod_alias directives. Anything using mod_rewrite will execute first. So before you do anything else, translate any and all existing mod_alias rules (Redirect by that name) to mod_rewrite format, and put them before the domain-name-canonicalization redirect. Which, incidentally, is best expressed as

RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]


This will be your only unconstrained redirect (meaning that the condition is evaluated on all requests) and it will be the very last redirect (the ones with [R=301,L] flag).

Right now, it looks as if some RewriteRule is making the server look for the nonexistent page before it ever reaches its intended redirect in mod_alias.

jp12

3:23 pm on Feb 24, 2014 (gmt 0)

10+ Year Member



where else could this rewrite rule be located? The Htaccess file is in the directory where the weather links are located....

g1smd

3:37 pm on Feb 24, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



With the pattern you have used it should be in root.

Path matching in mod_rewrite is on a "per-directory" basis.

lucy24

4:47 pm on Feb 24, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



First get rid of all mod_alias rules-- the ones beginning in "Redirect". Then see if any problems remain.