0

I am migrating from olddjango.com to newdrupal.com, and need a way to alter the URLs from the old style into the a format we've chosen for the new Drupal application.

In addition to rewriting them, I want the final "corrective" rule to redirect, to register the 301. The structure of the URL has a marker that I'm using as a RewriteCond, it is the last thing I change before the redirect (so the URI won't get rewritten unless it is from the old site).

I've written a small number of mod rewrite rules, which work well enough in http://htaccess.madewithlove.be/

If I put them in my /etc/httpd/conf/httpd.conf they work smashingly... but that isn't a viable option. (it theoretically COULD be... but only as a last resort)

However, when I put them in /.htaccess, they do absolutely nothing...

I put them at the top of the file, before anything else, I put them at the bottom... and in the middle. I tried with and without "RewriteBase /"...

I'm obviously missing something, or the nature of D7 disallows pre-empting the machinations of Clean URL. I've found at least one person saying it was possible, but no one saying there were any special tricks involved, or actual documentation... so I'm just trying to get an answer committed to the interwebs one way or the other.

This is the final rule of my section of rewrites:

RewriteCond %{REQUEST_URI} ^/?gbnk/?.* [NC]
RewriteRule ^/?gbnk/?(.*) $1 [NC,R=301,L]

  • Do you have all the regular .htaccess stuff setup? AllowOverride Yes, mod_rewrite etc? – Steven Feb 27 '14 at 21:34
  • Can you add an example rule or two? – mpdonadio Feb 27 '14 at 22:10
  • Using default .htaccess file, inserting rules at various points... most logically, immediately beneath #rewriteBase / – OhkaBaka Feb 28 '14 at 14:14
  • Clean URLs works. Rules work when in httpd.conf, so methinks mod_rewrite/AllowOverride themselves are fine. – OhkaBaka Feb 28 '14 at 14:15
  • This is my final rule in my block of rewrites: RewriteCond %{REQUEST_URI} ^/?gbnk/?.* [NC] RewriteRule ^/?gbnk/?(.*) $1 [NC,R=301,L] – OhkaBaka Feb 28 '14 at 15:17
2

Personally, I would see if the Redirect module works for your needs. Is it basically a UI interface for making 301 redirects. Off the top of my head, though, I don't recall if it will work with patterns.

If that doesn't work, then your best bet is to put them in the .htaccess right after the

# RewriteBase /

line.

Just remember that mod_rewrite will continue to process rules unless you use the L flag. You also want to explicitly give the response code with the R flag. Something like this should work

# RewriteBase /

RewriteRule ^some/path/(.*) /other/place/$1 [R=301,L]
RewriteRule ^another/hierarchy/(.*) /different/thing/$1 [R=301,L]

# Pass all requests not referring directly to files in the filesystem to
# index.php. Clean URLs are handled in drupal_environment_initialize().
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^ index.php [L]

Keep in mind that rewrites can be frustratingly hard to debug, but logging can help. Also make sure you are using rewrites and not redirects. Mixing mod_rewrite and mod_alias in same .htaccess will cause all sorts of grief because of process order.

  • I had [R] and added [L], but it didn't make a difference. Otherwise we're on the same page. I love Redirect, but it doesn't do patterns. – OhkaBaka Feb 28 '14 at 15:16
  • @OhkaBaka I made an edit to the exact rules (well, slightly different path parts) on my system, and everything worked on my server. Either your rules are incorrect, or something is intefering with the process. You need to turn on rewrite logging. – mpdonadio Feb 28 '14 at 15:30
  • It is getting clearer... I renamed .htaccess... and everything (Clean URLs specifically) is still working as expected. Near as I can figure, .htaccess is not even being procced. – OhkaBaka Feb 28 '14 at 18:42
  • Some ugly duplicate code turned up in httpd.conf... the drupal rewrite code was in there along with a second AllowOverride (this one set to None)... Glorious. Anyway thanks for the assist, wouldn't have gotten there without Logging (which I didn't know you could do). – OhkaBaka Feb 28 '14 at 20:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.