Mod_Rewrite Expert needed

subster

Elite Member
Joined
Apr 5, 2008
Messages
2,158
Reaction score
1,609
Hi there,

I am currently migrating to a new shop and every url switches to a new one.
I've this gathered and wanted to do it with a simple redirect 301, but unfortunately due to some mod_rewrite rules in the htaccess it wont work.

Problem:

This block of rules is crucial for the script to work properly:

Code:
	#RewriteRule ^(.*)(.+)\.html(.*)$ 								commerce_seo_url.php?linkurl=$1$2$3 [qsappend,L]

	RewriteCond %{REQUEST_FILENAME} !\.html$ [OR]
	RewriteCond %{REQUEST_FILENAME} !\.php$ [OR]
	RewriteRule ^(.*)$												commerce_seo_url.php?linkurl=$1 [qsappend,L]

If I paste now my redirects into the htacess the redirect works, but the targeted url gets an ?linkurl=/old/category at the end what leads the redirect into an 404.


Explained Example:

Pasted at the end of .htaccess:

redirect 301 /old/categorystructure http://www.domain.com/new/categorystructure/ (the old urls did not have a backslash at the end)

Result:
Code:
http://www.domain.com/new/categorystructure?linkurl=/alter/ordner
= 404 error

I search someone who could find the right syntax of a mod_rewrite rule to work this problem out.

It is urgent.

Thank you for your replies.
 
anyone? quick dollars to be made! you show me the working rule and I will pay for it.
 
put your redirects at the beginning of the htaccess file

if your old path doesn't end with .html or .php this rule gets executed which puts the linkurl=... at the end
Code:
	RewriteRule ^(.*)$           commerce_seo_url.php?linkurl=$1 [qsappend,L]
 
Thank you so much for your reply!
I pasted them into nearly every possible position.

Old urls doesnt have .html / .php endings and with the linkurl= suffix the redirects are leading into a 404...
 
I got a bit deeper into this issue:

the suffix ?linkurl= comes from the qsappend from this line:

Code:
RewriteRule ^(.*)$ commerce_seo_url.php?linkurl=$1 [qsappend,L]

I am damnsure the script needs this commad to work properly, so my only chance is to use [L,301] somewhere before this line like

Code:
RewriteRule ^old/(.*) http://example.com/new/$1/ [L,301]

But this results in an 500 error. Man, the whole day is going up in flames on this issue... frustatring..
 
I understand your frustration. I wish I could help. I know this comment seems worthless to you BUT it might make you feel a lil better knowing that my day, too, has gone up in flames. Good luck, man.
 
[301] is not a valid flag, that is why you get a 500 error

it should be something like this
Code:
RewriteRule ^old/(.*) http://example.com/new/$1/ [QSA,R=301,L]
 
Thanks for the headsup goodfor :)

Thanks for this final tip sockpoppet - Rep and thanks given!

One final question, If you'd be so kind to answer me this last one...

Code:
RewriteRule ^cat1/subcat1/(.*) http://example.com/new/$1/ [QSA,R=301,L]

works great for all my categories. But when I add a products url to redirect it give the 404 again. Where is the failure here:

Code:
RewriteRule ^cat1/subcat1/product-url-without-trailing-slash-or-other-ending/(.*) http://unchangedurl.com/cat1/newsubcat$1/ [QSA,R=301,L]

Additional question: I have to creat this rules for roundabout 5K product urls, will the performance get a hard hit by that?

Thanksthanksthanks - I am really greateful for your help and sharing your knowledge.
 
Every htaccess rule decreases performance.
 
works great for all my categories. But when I add a products url to redirect it give the 404 again.
Do you add the product url after your "category-rule"? that's the problem.
Your product url get matched by the "category-rule": "^cat1/subcat1/(.*)", so it never reaches the redirect for the product url.

Additional question: I have to creat this rules for roundabout 5K product urls, will the performance get a hard hit by that?
The problem with .htaccess files they get reread by apache on every request, if you have a high traffic site even small files could hit on the performance. So i wouldn't recommend a file with 5k redirects. You could put the redirects in your apache config, so they get read once.
I don't know how your urls look like, but the best way is to avoid the 5k redirects and find a rule that matches all of your product urls. If that is not possible you should use a php script for your redirects, i think that would be faster than mod_rewrite.
 
The Solution:


Catgoryurls

Code:
RewriteRule ^/maincat/subcat/? http://domain.de/neuemaincat/neuesubcat/ [R=301,L]

Producturls

Code:
RewriteRule ^maincat/subcat/produkturl/?$ http://domain.de/neuemaincat/neuesubcat/produkturl.html [R=301,L]



just if someone else is in the same need...
 
Back
Top