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
Apache Rewrite Suggestion
Give this a try. Place this code before the other rewrite rules.
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.
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:
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
RewriteEngine
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.
Rewriting old urls
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,
Try something
Try something like,