Forum Moderators: phranque

Message Too Old, No Replies

301 Redirect with dynamic URL

         

jonrojas13

5:54 am on Aug 27, 2015 (gmt 0)

10+ Year Member



I must say that I am stumped.

I have been trying to do a 301 redirect for a podcast rss feed url. Here is where I am so far... I think (and hope) I am getting close:

I am trying to redirect the old feed, example.com/?feed=podcast

to the new feed, example.com/feed/podcast

Here is the code I have added to my .htaccess. Am I close? Thanks!

RewriteCond %{QUERY_STRING} ^feed=podcast$ 
RewriteRule ^$ http\:\/\/www\.example\.com\/feed\/podcast\? [R=301,L]

Dammy

6:08 am on Aug 27, 2015 (gmt 0)

10+ Year Member



what result did got with this?

lucy24

7:23 am on Aug 27, 2015 (gmt 0)

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



Am I close?

Yes. But you don't need to escape anything in the target, so get rid of all those backslashes. Note too that the rule as written will only work if "feed=podcast" is the only thing in your query string. If that really is the way your old URLs looked, then you are good to go. Otherwise things get more complicated.

Quick edit: This is assuming you've already got "RewriteEngine on", and any other lines necessary for mod_rewrite.

jonrojas13

1:32 pm on Aug 27, 2015 (gmt 0)

10+ Year Member



This is assuming you've already got "RewriteEngine on", and any other lines necessary for mod_rewrite.


Correct. I do have "RewriteEngine on".

After removing the backslashes, example.com/?feed=podcast still goes to that specific page. It doesn't appear the redirect is working. :(

whitespace

2:23 pm on Aug 27, 2015 (gmt 0)

10+ Year Member Top Contributors Of The Month



Correct. I do have "RewriteEngine on".


and any other lines necessary for mod_rewrite.


So, we can assume .htaccess/mod_rewrite is good to go?

Do you have any other directives in your .htaccess file? (I assume you do since you have a nice friendly URL.) The order is important. Generally, external redirects should always come at the top, before your internal rewrites.

jonrojas13

3:00 pm on Aug 27, 2015 (gmt 0)

10+ Year Member



So, we can assume .htaccess/mod_rewrite is good to go?

I'm not sure... I don't see a .htaccess/mod_rewrite, but my other 301 redirects are working.

Here is my entire .htaccess file"


RewriteEngine on

RewriteCond %{QUERY_STRING} ^feed=podcast$
RewriteRule ^$ http://www.example.com/feed/podcast? [R=301,L]

RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^feed\=podcast$ "http\:\/\/www\.example\.com\/feed\/podcast" [R=301,L]

RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^feed$ "http\:\/\/example\.com\/feed\/podcast" [R=301,L]


# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

whitespace

3:58 pm on Aug 27, 2015 (gmt 0)

10+ Year Member Top Contributors Of The Month



I don't see a .htaccess/mod_rewrite ...


Sorry, I meant .htaccess and mod_rewrite.

Well, your .htaccess looks OK, although you should obviously remove your other two attempts.

And to confirm, the URL you are requesting is.... ?

http://www.example.com/?feed=podcast


...but my other 301 redirects are working.


Maybe just terminology, but you don't actually have any other "301 redirects" in your .htaccess file (apart from what looks like your attempts at this particular redirect). I assume you really mean "rewrites" - so WordPress is otherwise working OK? Or do you really have some other 301 redirects somewhere else? Server config? Another .htaccess file perhaps?

lucy24

5:22 pm on Aug 27, 2015 (gmt 0)

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



Uh-oh, you've got WP on the same site? That makes everything tricky. In particular: the URL
example.com/feed/podcast
doesn't correspond to a real, physical file, does it? That means that even if the redirect worked, you'd still end up in WordPress because of the -f test. Is this, in fact, a URL that your WP install is already set up to deal with?

Simple experiment.
RewriteRule foobar http://www.example.com/widget.html [R=301,L]
AND, in the WordPress block, add this line before the -f condition:
RewriteCond %{REQUEST_URI} !widget
Where I've said "foobar" and "widget", use any two made-up names that don't really exist on your site. Now try requesting /foobar.html (or anything with "foobar" in the name). If the rule works, you will end up on a 404 page, and the browser's address bar will say /widget.html

Does "other 301 redirects" mean redirects using mod_alias (Redirect or RedirectMatch by that name)?

jonrojas13

5:44 pm on Aug 27, 2015 (gmt 0)

10+ Year Member



First off, I wanted to thank everyone in this thread for helping me. It's amazing that people spend time like this and I am very appreciative!

In particular: the URL example.com/feed/podcast doesn't correspond to a real, physical file, does it?

I'm not too sure. We have a wordpress site and use the Powerpress plugin to generate an RSS feed. I'm assuming staying in WP would be ok? Our example.com/feed/podcast seems like it is still within WP, but takes a user to the XML file (in Chrome at least)

Does "other 301 redirects" mean redirects using mod_alias (Redirect or RedirectMatch by that name)?

I've done the other "301 redirects" through the GUI on Bluehost. I go into the 'Domains' section and choose 'Redirect'. It appears that the .htaccess file is automatically updated when I make the changes in the GUI (cpanel).

lucy24

6:33 pm on Aug 27, 2015 (gmt 0)

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



I'm not too sure.

It is VERY unlikely that you have extensionless files on your server, so I may be surer than you are ;) It's important to understand the difference between "real, physical file" and "resolves to a valid page", because your average CMS-- including WordPress-- is built on this distinction.

Any redirects that you add manually will apply before WP does its thing, assuming the redirect comes before-- that is, physically "above" on the page-- the WP section of your htaccess. (The <IfModule> envelope doesn't affect execution order the way, for example, a <FilesMatch> would.)

It appears that the .htaccess file is automatically updated when I make the changes in the GUI (cpanel).

Uh-oh, this sounds perilous. If you're going to start editing your htaccess file directly-- which is definitely A Good Thing-- you should probably stop using alternative methods that result in third-party edits to that same htaccess. If you do create a redirect using your Control Panel, what do you see in htaccess? Is it something beginning in RewriteRule, or something beginning in Redirect(Match)?

jonrojas13

6:42 pm on Aug 27, 2015 (gmt 0)

10+ Year Member



Is it something beginning in RewriteRule, or something beginning in Redirect(Match)?

Both of these RewriteRules were created in the cpanel:
RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^feed\=podcast$ "http\:\/\/www\.example\.com\/feed\/podcast" [R=301,L]

RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^feed$ "http\:\/\/example\.com\/feed\/podcast" [R=301,L]

whitespace

10:09 pm on Aug 27, 2015 (gmt 0)

10+ Year Member Top Contributors Of The Month



I'm always a bit horrified at the code cPanel "spits" out. (And it does not always work.)

You say these two redirects work as intended? One redirects to "example.com", whilst the other redirects to "www.example.com" - this strikes me as odd.

Can you describe in English what you think these two redirects should be doing.

(The point is that if these two redirects "work" then the new redirect, that comes first, should also work.)

lucy24

1:32 am on Aug 28, 2015 (gmt 0)

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



Yes, I suppose they've got a reason for saying
RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com$
rather than simply
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$

or, heck, first checking whether the site even has subdomains (surely cpanel has a way to know this?) and then reducing to
RewriteCond %{HTTP_HOST} example\.com
and omitting even this line if there's only one site on the account.

Both of these RewriteRules were created in the cpanel:

In each case, what did you tell cpanel in order to make it generate these specific rules? (I don't know a whole lot about cpanel, since my host rolls their own and I don't think they even do redirects, apart from domain-name canonicalization.)

:: wait, stop, rewind ::

Does that mean that cpanel itself is responsible for all those \ superfluous escape-slashes in the target? The mind boggles.