Forum Moderators: phranque

Message Too Old, No Replies

Redirect/Rewrite one url for me!

I can't figure this one out.

         

velcrobots

5:29 pm on Oct 2, 2011 (gmt 0)

10+ Year Member



I need to redirect (or rewrite, whatever works, I'm so confused) a url for a feed from my old cms to my new one.

The old feed could be found here:
[ainonline.com...]

And now it's found here:
[ainonline.com...]

How can I do this?

g1smd

5:36 pm on Oct 2, 2011 (gmt 0)

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



Use a 301 redirect.

It's one line of code. Let's see you best shot.

There's 50 000 prior threads with examples.

velcrobots

5:53 pm on Oct 2, 2011 (gmt 0)

10+ Year Member



OK, this is what I've been doing. We have around 15 feeds, each just XML files at the old root. They're now all at "taxonomy/term/#/feed" except the first one. All of these are working except the one above, what I tried is below (first one). But I used ReWrite - is that bad? They only need to be like this for a week or two.

Trust me, I've been reading about this all morning! I read one article and I think I got it, then I read another that seems to say the opposite.

# RSS Redirects
RewriteRule ^index\.php\?id\=5$ /?q=latest-news/feed
RewriteRule ^Business_Aviation.xml$ /?q=taxonomy/term/9/feed
RewriteRule ^Air_Transport_and_cargo.xml$ /?q=taxonomy/term/6/feed
RewriteRule ^Defense.xml$ /?q=taxonomy/term/13/feed
RewriteRule ^Accidents_Safety_Security_and_Training.xml$ /?q=taxonomy/term/1/feed
RewriteRule ^Airports_Heliports_and_FBOs.xml$ /?q=taxonomy/term/7/feed
RewriteRule ^Avionics_and_ATC.xml$ /?q=taxonomy/term/8/feed
RewriteRule ^Cabin_Interior_and_Electronic.xml$ /?q=taxonomy/term/11/feed
RewriteRule ^Charter_and_Fractional.xml$ /?q=taxonomy/term/12/feed
RewriteRule ^Financing_Insurance_and_Taxes.xml$ /?q=taxonomy/term/14/feed
RewriteRule ^General_Aviation.xml$ /?q=taxonomy/term/15/feed
RewriteRule ^Maintenance_and_Modifications.xml$ /?q=taxonomy/term/16/feed
RewriteRule ^People.xml$ /?q=taxonomy/term/17/feed
RewriteRule ^Regulations_and_Government.xml$ /?q=taxonomy/term/19/feed
RewriteRule ^Rotorcraft.xml$ /?q=taxonomy/term/21/feed

lucy24

7:38 pm on Oct 2, 2011 (gmt 0)

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



Redirect and rewrite are different things, though they both use mod_rewrite. What do you want the user's address bar to show-- the old URL or the new one? Where does the "real" content live-- the old location or the new one?

Either way, each Rule needs to end in [L]. Here it is not catastrophic to leave it out, since all your URLs are mutually exclusive, but get in the habit anyway.

After the "week or two" are they all moving back to their original location? If so, [R=302] or plain [R] probably is appropriate. Otherwise [R=301]. That's assuming you do need to redirect, not just rewrite.

Your first Rule is not working because a RewriteRule can't see the query string (the part after the question mark). So that one needs a preceding RewriteCond [httpd.apache.org] that looks at %{QUERY_STRING}

velcrobots

8:08 pm on Oct 2, 2011 (gmt 0)

10+ Year Member



Thanks Lucy. I'm slowly learning this stuff.

I need the address bar to show the old url (index.php?id=5). This is all because we have an iPhone app that's fed from these feeds and they're hard-coded in, until we get the new version out there.

lucy24

10:25 pm on Oct 2, 2011 (gmt 0)

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



OK, so you're just doing a rewrite. That means no [R] of any kind. But read up on the QueryString part.

As a general rule-- this is for personal sanity-saving, not to make htaccess work differently-- leave a blank line after each RewriteRule so you can readily pick them out. Here I wouldn't bother for the ones that are all the same. But do separate the first one, if only to remind yourself that the RewriteCond applies only to the immediately following Rule. Again that's for your personal information. .htaccess doesn't seem to care about blank lines, though it takes spaces very, very seriously ;)

Oh, and I kinda think that the Rewrites will magically change into Redirects if they start with a leading slash. Officially it's supposed to be the whole
http://www.example.com
(protocol plus full address)
but there's a permitted shortcut in mod_alias [httpd.apache.org]* that appears to work in mod_rewrite too. Does for me, at least.

Anyway, if you've got Stuff Going On with your server, make it as unambiguous as possible.


* "The new URL should be an absolute URL beginning with a scheme and hostname, but a URL-path beginning with a slash may also be used, in which case the scheme and hostname of the current server will be added." Disregard the rest, because mod_alias and mod_rewrite behave differently.

g1smd

11:32 pm on Oct 2, 2011 (gmt 0)

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



First off, add the [L] flag to every rule unless you want random crazy failures on your site.


RewriteRule ^index\.php\?id=5$ /?q=latest-news/feed [L]


Your rewriterule pattern can never match an incoming URL request because RewriteRule can only see the path part of the requested URL. You MUST use a preceding RewriteCond looking at QUERY_STRING in order to obtain the requested query string value.


RewriteRule ^Business_Aviation.xml$ /?q=taxonomy/term/9/feed [L]


Underscores in URLs are always a bad idea. You must also escape the literal period in the pattern.

To be clear what you want, the above rule says that if something asks for
example.com/Business_Aviation.xml
then the server will look for content in
/index.php?q=taxonomy/term/9/feed


There is at least one problem there, the biggie being unescaped slashes are not a valid or allowed character in a query string according to the HTTP specs.

velcrobots

12:35 am on Oct 3, 2011 (gmt 0)

10+ Year Member



To be clear what you want, the above rule says that if something asks for example.com/Business_Aviation.xml then the server will look for content in /index.php?q=taxonomy/term/9/feed


Yes, exactly it. And it's working - the iPhone app that looks for those old _xml files is successfully pulling from the new Drupal feeds at taxonomy/term/#/feed. Of course, except for the first one, the latest news.

So if I do this:
RewriteCond %{QUERY_STRING} 
RewriteRule ^index\.php\?id=5$ /?q=latest-news/feed [L]


Is that right? So {QUERY_STRING} just directs Apache to include the query string in the match?

lucy24

1:39 am on Oct 3, 2011 (gmt 0)

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



There is at least one problem there, the biggie being unescaped slashes are not a valid or allowed character in a query string according to the HTTP specs.

Happily these are all individual, hand-crafted rewrites (not redirects), so if necessary the / can be replaced with some other character -- or even %2F -- and the php file will make everything right :)

Is that right? So {QUERY_STRING} just directs Apache to include the query string in the match?

Er, no. Let me just paste in the whole boilerplate and you can pick out the parts you need.

Query Strings

The Query String, also known as a Parameter, is the part of an url after the question mark. Question = query.

By default, rewrites simply ignore the query string. That is, mod_rewrite stashes the query in a safe place, does its stuff to the part before the question mark, and then reappends the original query.

Changing a Query

#1 To delete a query, add a ? to the end of your rewrite target.
#2 To replace a query—or create a new one—add ?blahblah to the rewrite target. The blahblah can be either literal text, or stuff you captured earlier. (#1 and #2 are really the same thing: you're just replacing the query with either something or nothing.)
#3 To add to an existing query, again put ?blahblah at the end of the target, but also add [QSA] to your flags (the bracketed items at the end of the Rule). It stands for "Query String Append", meaning that the blahblah is to be added to the existing query—if any—instead of replacing it.

Getting the Query

You only need to retrieve the original query if
#1 you want the rewrite to behave differently depending on what the query was
or
#2 you need to change or delete the query

Add a Condition that says

RewriteCond %{QUERY_STRING} blahblah


using your ordinary Regular Expressions, anchors and ! as needed.

To test whether there was a query at all

RewriteCond %{QUERY_STRING} .


which simply means "If the query contains at least one character of any kind".

If you need to capture any of the query, use parentheses as usual. In the rewrite target, the captures will be %1, %2 etc instead of $1, $2 etc, because they are coming from a Condition instead of the Rule. Each set is separately numbered, so the first capture from the Rule will still be $1.

g1smd

6:14 am on Oct 3, 2011 (gmt 0)

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



Is that right? So {QUERY_STRING} just directs Apache to include the query string in the match?
No. The pattern in the RewriteRule must contain only the path part of the URL. RewriteRule itself cannot "see" or match the protocol, domain, port number or query string. You have to put the pattern for the query string part of the request in a RewriteCond directly above the particular RewriteRule that it will apply to.

velcrobots

9:34 am on Oct 3, 2011 (gmt 0)

10+ Year Member



OK. So RewriteCond can match the query string and store it (that's what the %1 is? sort of the variable name?) And then RewriteRule can use that?

Like this? Do I need to escape the = sign?

RewriteCond %{QUERY_STRING} ^id=5$
RewriteRule ^index\.php%1 /?q=latest-news/feed

velcrobots

9:47 am on Oct 3, 2011 (gmt 0)

10+ Year Member



Got it! This is working:

RewriteCond %{QUERY_STRING} ^id=5$
RewriteRule ^index\.php$ /?q=latest-news/feed [L]

lucy24

4:20 pm on Oct 3, 2011 (gmt 0)

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



Bingo.

In English: If the existing query string consists entirely of "id=5", rewrite to index page with new query string "latest-news/feed". If that's what you wanted, you've got it :) (No, an equals sign doesn't need to be escaped. It has no extra meaning either in htaccess or in generic RegEx. It might need escaping in contexts like Javascript or php that use mathematical operators.)

And keep your fingers crossed about all those slashes in the query strings. As long as they're not allowed to run wild in the public Internet, they probably won't make your server explode. But if it's used in a permanent address, change it.