Forum Moderators: phranque

Message Too Old, No Replies

301 redirect in htaccess - format to use

         

bob451

4:56 pm on Feb 25, 2016 (gmt 0)

10+ Year Member



On a Drupal site trying to setup 301 redirects in htaccess...Anyone know how to format the following: Uses same Domain for both old and new...no extensions on the files hames... the newfilename is an alias

Redirect 301 /oldsubdirectory/oldfilename http://example.com/newsubdirectory/newfilename

Can't get it to work using the above pattern...

Any help appreciated.

Bob

whitespace

7:13 pm on Feb 25, 2016 (gmt 0)

10+ Year Member Top Contributors Of The Month



You will probably need to use mod_rewrite (RewriteRule) instead of mod_alias (Redirect) since I'm pretty sure Drupal will already be using mod_rewrite for routing the request.

Different Apache modules run at different times. The request is probably being routed before your Redirect gets a chance, so never gets executed.

Try something like the following after your existing RewriteEngine directive and before any other Drupal directives.


RewriteRule ^oldsubdirectory/oldfilename$ http://example.com/newsubdirectory/newfilename [R=301,L]

lucy24

7:20 pm on Feb 25, 2016 (gmt 0)

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



Redirect 301

There's your problem right there. Drupal, like any CMS you can name, is built around mod_rewrite. Rules beginning in "Redirect" use mod_alias. The two mods don't play nice together, so you'll need to change any existing mod_alias redirects to use mod_rewrite syntax instead. If there are a lot of them, there are a couple of quick find-and-replaces to make the change globally.

Short version, assuming there's just one specific file involved:
mod_alias
Redirect 301 /oldsubdirectory/oldfilename http://example.com/newsubdirectory/newfilename
mod_rewrite
RewriteRule ^oldsubdirectory/oldfilename http://example.com/newsubdirectory/newfilename [R=301,L]

Put this rule immediately before the part of your htaccess containing the Drupal rules. You may need to say "RewriteEngine on" twice, which looks silly but does no harm.

bob451

7:54 pm on Feb 25, 2016 (gmt 0)

10+ Year Member





The RewriteRule by itself gave me a 404.

The Short version using just one specific file and adding the Rewrite Engine on at the top before the Drupal rules results in Internal Server Error.

Thanks for the responses. I'll keep at it.

whitespace

8:14 pm on Feb 25, 2016 (gmt 0)

10+ Year Member Top Contributors Of The Month



The RewriteRule by itself gave me a 404.


But it did redirect?

If you access "http://example.com/newsubdirectory/newfilename" directly in the browser, does it work?

The Short version using just one specific file and adding the Rewrite Engine on at the top before the Drupal rules results in Internal Server Error.


RewriteEngine is one word. A 500 internal server error suggests you have the syntax wrong (check your error log for details).

You may need to say "RewriteEngine on" twice


RewriteEngine only needs to appear once and can appear anywhere in .htaccess, even at the very bottom (which is a bit weird). Who said it was top-down?

lucy24

8:36 pm on Feb 25, 2016 (gmt 0)

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



Who said it was top-down?

?! It's strongly implied by the docs, since they offer "RewriteEngine off" as an alternative to commenting-out lots and lots of rules. I always thought it meant you could switch things off and on at will, line by line.

Granted, the docs do not always correspond precisely to reality. (I think it was you who discovered that what they say about external redirects doesn't precisely correspond to what they do.) Something I should probably keep in mind when citing the horse's mouth [apache.org] in any context.

:: wandering off to experiment on test site ::

Criminy. You learn something new every day.

RewriteMap directives of the type prg are not started during server initialization if they're defined in a context that does not have RewriteEngine set to on

Nothing to do with the present discussion, but if I paste it in here I'll be more likely to remember in future. So you need to have the RewriteEngine enabled just to define a map, even if you're not planning to use it right then and there.


Edit:
Bob, back to you. The foregoing means that I was mistaken and you do not need to say RewriteEngine on more than once. If Drupal behaves like WordPress, they've got a section of your htaccess that's delimited with something like "# BEGIN DRUPAL" and you shouldn't change anything inside this section. If you want to add RewriteRules of your own, put them immediately before the Drupal section, because typically your own rules will involve external redirects* while the CMS involves internal rewrites.

When you get the 404, what does your browser's address bar say?


* "External" here doesn't mean "go to some other site". It just means "tell the visitor to take suchandsuch action, as opposed to doing it secretly yourself behind the scenes".

bob451

9:01 pm on Feb 25, 2016 (gmt 0)

10+ Year Member



I use the engine on command as follows: "RewriteEngine on"
I found nothing in the logs.

Yes, if I access the newfile url directly in a browser, it does display properly.

Thanks again...

whitespace

9:56 pm on Feb 25, 2016 (gmt 0)

10+ Year Member Top Contributors Of The Month



Who said it was top-down?
?! It's strongly implied by the docs,


I'm sure it's even mentioned somewhere in the docs? It is top-down for pretty much everything, although there is the odd exception. RewriteBase is another. (If you have multiple RewriteBase directives - although that's really invalid - then the last one wins and controls the entire file.)

they offer "RewriteEngine off" as an alternative to commenting-out lots and lots


Hhhmm, although this has been clarified a bit in the 2.4 docs.

Use this directive to disable rules in a particular context, rather than commenting out all the RewriteRule directives.


Although you could just comment out the "RewriteEngine On" directive (since "Off" is default)? Incidentally, an "On" trumps an "Off" (regardless of order), so you would need to remove/change the "On".

whitespace

9:59 pm on Feb 25, 2016 (gmt 0)

10+ Year Member Top Contributors Of The Month



Yes, if I access the newfile url directly in a browser, it does display properly.


So, the redirect didn't work properly...

When you get the 404, what does your browser's address bar say?


And maybe copy/paste the relevant section from your .htaccess file?

lucy24

10:00 pm on Feb 25, 2016 (gmt 0)

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



Yes, if I access the newfile url directly in a browser, it does display properly.
OK, good, now what happens if you request the oldfile URL? Earlier you said you got a 404. The part that wasn't clear is whether you got the 404 at the old URL or the new one. It sounds like old URL, meaning the redirect didn't take place and we need to figure out why.

Edit: We're not doing this to confuse you. We're just reading the boards at exactly the same time, probably with multiple tabs open (speaking for myself, at least). I do envy that one well-known Forums software package that alerts you if someone else has added to a thread while you were typing your own post :)