Hi everybody.
I have a simple question, regarding the .htacces in Drupal. I want to redirect my users from my old URLs (from the time before I used drupal) to the new URLs in Drupal. Generally I use the following:

Redirect /old-url.php http://www.domain.com/new-url

But with my current settings, this does not work and redirects me to something like this:

Redirect /old-url.php http://www.domain.com/new-url?q=old-url.php

The reason is the rewrite rule in my .htacces:

 # Rewrite current-style URLs of the form 'index.php?q=x'.
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?q=$1 [L,QSA] 

BUT: I cannot change this rule, otherwise my website does not work properly anymore. Does anyone know, what I can do, to redirect my visitors (and search engines) from my old URL to the new URL without changing this exact and important URL??

Any Idea and Help Is Highly Appreciated!!!
Thank you very much!
Jab

Comments

allella’s picture

Give this a try. Place this code before the other rewrite rules.

  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^old-url.php$ /new-url [R=301,L]

The ^old-url.php$ specifies the pattern should match the exact page old-url.php The ^ specifies the 'start of line' and the $ is the 'end of line'.

Of course, you can, and probably should, use regular expressions in place of old-url.php to do much more dynamic pattern matching.

The /new-url specifies the substitute path as /new-url The leading / is necessary or else Apache will append the DOCUMENT_ROOT variable before the intended substitute path.

The [R=301,L] says to do a 301 redirect and stop processing any rewrite rules which come after this one.

My situation was similar, but had some key differences. So, here's a slightly more advanced example. Disclaimer: I don't claim to be expert in mod_rewrite and any improvements to this code are welcome.

I'm working with a virtual host and wanted to apply a different rule based on the domain name. One way to accomplish this is to add an extra RewriteCond which checks the current host / domain name.

  RewriteCond %{HTTP_HOST} (www.)?drupal.org
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(.*)\.asp(.*)$ http://drupal.org/foobar? [R=301,L]

The example directly above says to check Apache's HTTP_HOST variable (e.g. the portion of the domain name between http:// and before the first / ) and see if it matches a specified domain. In this case, I'm using a ? (question mark) regular expression to match drupal.org with or without a www. , so this would match www.drupal.org or drupal.org

Then, do the normal checks that the url isn't already a valid file RewriteCond %{REQUEST_FILENAME} !-f and is not a valid directory RewriteCond %{REQUEST_FILENAME} !-d. RewriteConds listed one after another are implicitly linked with AND, so all three conditions must pass for the RewriteRule to actually rewrite the url.

Finally, I rewrite anything with a .asp in the url and redirect it to a page. Broken down it's:

  • ^ -- the start of line
  • (.*) -- 0 or more of any character
  • \. -- a literal period. Without the \ it would be interpreted as a 'any single character'
  • asp
  • (.*) -- 0 or more of any character (this will pick up anything after .asp, including query strings like ?x=100)
  • $ -- end of line
  • http://drupal.org/foobar -- the redirect address
  • ? -- don't append the query string from the original address
  • [R=301,L] -- do a 301 redirect and stop processing any rewrite rules

Note, the ? after foobar is necessary if you want to remove the query string from the end of the substitute path. For example, without the ? , a visitor to foobar.asp?x=100 would be directed to http://drupal.org/foobar?x=100 . With the ? , the x=100 is chopped off.

Note 2, the redirect page needn't be static. You could redirect to something like http://drupal.org/$1? where $1 is the first regular expression region (in this case, whatever comes before .asp)

I used http://httpd.apache.org/docs/trunk/rewrite/rewrite_intro.html for a simple explanation of regular expressions and Apache's rewrite documentation http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html#rewriterule for an explanation of mod_rewrite stuff.

Jim

OrangeCoat.com

deegenerate’s picture

Hi,

I did get the Apache Rewrite to work, but I needed to add one more line of code to "orangecoat"'s code:

// first line is the new line
RewriteEngine On
RewriteCond %{HTTP_HOST} (www.)?drupal.org
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.asp(.*)$ http://drupal.org/foobar? [R=301,L]

Once I did that, and uncommented the following line is httpd.conf:

LoadModule rewrite_module modules/mod_rewrite.so

It worked.

multicoder’s picture

Hi orangecoat-ciallella ,
Thanks for the excellent knowledge sharing!But I am very much new to the .htaccess and url rewritings in php, you can think of me as child also!
Well my problem is I have to redirect the old urls i.e. of existing site's url to new url of new site instead.
Eg.
http://www.domain1.com/test.php?letter=January
should map to
http://ver2.domain2.com/test/January
.Please help me with this.
Thanks & Regards,

allella’s picture

Try something like,

RewriteCond %{HTTP_HOST} www.domain1.com
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.php?letter=(.*)$ http://ver2.domain2.com/$1/$2? [R=301,L]