Forum Moderators: phranque

Message Too Old, No Replies

.htaccess 301 redirect

         

web_young

7:59 pm on Jan 6, 2009 (gmt 0)

10+ Year Member



I'm new to .htaccess and need to know how to 301 redirect this page:

/layout9.asp?id=322&page=8978

to this page:

/index.php/panther-lake-course-information

Any help is greatly appreciated.

wildbest

8:31 pm on Jan 6, 2009 (gmt 0)

10+ Year Member



# MOD_ALIAS PERMANENT REDIRECT #
Redirect 301 /layout9.asp?id=322&page=8978 http://www.example.com/index.php/panther-lake-course-information

But... are you sure your .asp pages are hosted on Apache server?

web_young

8:37 pm on Jan 6, 2009 (gmt 0)

10+ Year Member



No, the site is being redesigned and converted from .asp to .php, does the page have to actually exist on the server?

Thanks for your help.

web_young

8:40 pm on Jan 6, 2009 (gmt 0)

10+ Year Member



wildbest, i tried your code but it didn't work, is that because the .asp page doesn't exist on the server?

wildbest

8:43 pm on Jan 6, 2009 (gmt 0)

10+ Year Member



You can use .htaccess file only on Apache web server.

web_young

8:49 pm on Jan 6, 2009 (gmt 0)

10+ Year Member



Yes, the new site is on an apache server, but the old asp files don't exist on the new server. Can I still do a htaccess redirect in this situation?

wildbest

8:54 pm on Jan 6, 2009 (gmt 0)

10+ Year Member



Yes, you can.

to this page:

/index.php/panther-lake-course-information

I don't see a page there.
Do you actually mean?
/panther-lake-course-information/index.php

web_young

8:55 pm on Jan 6, 2009 (gmt 0)

10+ Year Member



wildbest your code works if I take the URL variables out, but how can I do it with the url variables?

wildbest

9:07 pm on Jan 6, 2009 (gmt 0)

10+ Year Member



RedirectMatch 301 ^/layout9\.asp\?id=322&amppage=8978$ http://www.example.com/panther-lake-course-information/index.php

Caterham

9:09 pm on Jan 6, 2009 (gmt 0)

10+ Year Member



but how can I do it with the url variables?

As the documentation [httpd.apache.org] states

mod_alias is designed to handle simple URL manipulation tasks. For more complicated tasks such as manipulating the query string, use the tools provided by mod_rewrite.

You can't check for an existing query_string with mod_alias. You can only substitute a new one.

You'll need a RewriteCond checking for %{QUERY_STRING}, the pattern of the RewriteRule directive matches against a local filepath in per-directory context. See the documentation [httpd.apache.org] for more details.

[edited by: Caterham at 9:11 pm (utc) on Jan. 6, 2009]

jdMorgan

9:14 pm on Jan 6, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



mod_alias isn't so good about seeing query strings attached to the URL. If the above does not work, try mod_rewrite:

Options +FollowSymLinks
RewriteEngine on
#
RewriteCond %{QUERY_STRING} ^id=322&page=8978$
RewriteRule ^layout9\.asp$ http://www.example.com/panther-lake-course-information/index.php? [R=301,L]

You may or may not need the first line (Options) on your hosting account, but only testing can tell you for sure.

Jim

web_young

9:18 pm on Jan 6, 2009 (gmt 0)

10+ Year Member



Thank you all for your help! jdMorgan, that did the trick. Thanks again!

wildbest

9:23 pm on Jan 6, 2009 (gmt 0)

10+ Year Member



I'm always trying to avoid using mod_rewrite if simple redirect tasks can be solved by mod_alias Redirect or RedirectMatch.

Caterham

10:31 pm on Jan 6, 2009 (gmt 0)

10+ Year Member



I'm always trying to avoid using mod_rewrite if simple redirect tasks can be solved by mod_alias Redirect or RedirectMatch.

Which you should always do (KISS [en.wikipedia.org]). But unfortunately none directive provided by mod_alias can check r->args but only r->uri.

wildbest

10:54 pm on Jan 6, 2009 (gmt 0)

10+ Year Member



RedirectMatch
The supplied regular expression is matched against the URL-path.

mod_alias is designed to handle simple URL manipulation tasks. For more complicated tasks such as manipulating the query string, use the tools provided by mod_rewrite.

That's what mod_alias documentation says. So yes, you're correct. Obviously, just checking the query string is already kind of manipulation!

g1smd

11:10 pm on Jan 6, 2009 (gmt 0)

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



You should not mix directives from Mod_Alias (
Redirect
and
RedirectMatch
) with directives from Mod_Rewrite (
RewriteRule
) within the same .htaccess file, because you cannot guarantee the order in which they will be processed.

In view of that, I only ever use

RewriteRule
for redirects and rewrites.

jdMorgan

11:12 pm on Jan 6, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The point is that a query string is not part of a URL (or URI) -- It is data *attached* to the URL to be passed to the resource (e.g. script) *at* that URL.

Another way to look at it is that a URL *locates* a resource on the Web. The query string plays no part in the location of the script, and is therefore not part of the URL.

mos_alias was written when dynamic Web sites were very rare or non-existent, and all (or most) based on primitive server-side includes (see mod_includes).

Jim