1

Hi guys i need help with 301 redirect using .htaccess

http://www.example.com/dir1/dir2/dir3/11&name=ben

to be redirected to

http://www.example.com/dir1/dir2/dir3/11

So far I have tried this

RewriteRule ^dir1/dir2/dir3/\d+&(.+) /dir1/dir2/dir3/$2 [R=301,L]

it works but redirects me to

www.example.com/dir1/dir2/dir3 without the number(11);

The second one is to redirect (dir/China to dir/china), (dir/London to dir/london) - Uppercase to lowercase

Thanks for your help.

BenI
  • 93
  • 1
  • 8

1 Answers1

2

This should work:

RewriteRule ^(dir1/dir2/dir3/\d+)\&.+$ /$1 [R=301,L,NC]

For lowercase conversion:

RewriteCond %{REQUEST_URI} [A-Z]
RewriteRule ^(serviced-apartments/.*)$ ${lc:$1} [R=301,L,NC]

Assuming RewriteMap for lc is correctly defined in Apache config.

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • Didn't work for me..display error message saying "This webpage has a redirect loop" – BenI Sep 16 '13 at 16:53
  • Yes that's very close, error message is gone. The problem now is all the 111, 222, 33 values redirect to 1, 2 and 3 respectively. It should be in this form: http://www.example.com/dir1/dir2/dir3/11&name=ben to http://www.example.com/dir1/dir2/dir3/11, http://www.example.com/dir1/dir2/dir3/21&name=ken to http://www.example.com/dir1/dir2/dir3/21 and so on – BenI Sep 16 '13 at 17:02
  • That seems to be working OK. Thanks a lot anubhava. What could I have done without you. Also I have another query, to redirect from all matching uppercase files to lowercase path (e.g: dir/London to dir/london).. – BenI Sep 16 '13 at 17:18
  • Upper case to lower case will require you to make a change in Apache config. Do you have access to that? – anubhava Sep 16 '13 at 17:26
  • Yes I do have access to the httpd.conf file – BenI Sep 16 '13 at 17:27
  • Ok for lowercase see this answer of mine: http://stackoverflow.com/questions/5740022/how-to-add-text-to-urls-using-htaccess/5740137#5740137 – anubhava Sep 16 '13 at 17:41
  • I have tried that and it didn't work for me. In the conf file I have; RewriteMap lc int:tolower and in htaccess I have Options +FollowSymlinks -MultiViews RewriteRule ^(dir1.*)$ /${lc:$1} [R=301,L]. I would ideally like the to redirect all www.example.com/dir1/File to www.example.com/dir1/file – BenI Sep 17 '13 at 08:20
  • Assuming Apache has been bounced after adding `RewriteMap` line you can use this rule `RewriteRule ^(dir1/.*)$ /${lc:$1} [R=301,L]` – anubhava Sep 17 '13 at 09:20
  • Hmm ok, try this rule: `RewriteRule ^(?=.*?[A-Z])(dir1/.*)$ /${lc:$1} [R=301,L]` – anubhava Sep 17 '13 at 09:49
  • It redirects all to home page now – BenI Sep 17 '13 at 10:04
  • That was actually tested code on my side. Try in a different browser. – anubhava Sep 17 '13 at 10:05
  • could it be from my virtual host conf? I have done everything and it still wouldn't work. RewriteEngine On RewriteMap lc int:tolower RewriteCond %{REQUEST_URI} [A-Z] RewriteRule ^(?=.*?[A-Z])(serviced-apartments/.*)$ /${lc:$1} [R=301,L] – BenI Sep 17 '13 at 10:52
  • Yes it could very well be. I will edit the answer for one last attempt. If it doesn't work open a new question to get better answers. – anubhava Sep 17 '13 at 10:54
  • I have figured it out. I removed slash from dir1/ to dir and it worked. Thanks alot – BenI Sep 17 '13 at 11:19
  • sorry to border you again, how would you redirect www.example.com/mylist-12 to /our-list, www.example.com/mylist-110 to /our-list – BenI Sep 17 '13 at 11:59
  • Try `RewriteRule ^mylist-[0-9]+$ /our-list [L]` – anubhava Sep 17 '13 at 12:11