-2

Basically my current link is:

https://www.mysite.co.uk/segment1/segment2/segment3/segment4/segment5/segment6/segment7

how can i rewrite this url to:

https://www.mysite.co.uk/segment1/segment2/segment3/segment4/segment5/segment6-mystring/segment7

I have tried:

RewriteRule ^\/segment1\/segment2\/(.*)\/(.*)\/(.*)\/(.*)\/(.*)\/(.*) \/segment1\/segment2\/$3\/$4\/$5\/$6-mystring\/$7 [r=301,L]
anubhava
  • 761,203
  • 64
  • 569
  • 643
user2171245
  • 61
  • 2
  • 10
  • Possible duplicate of [Tips for debugging .htaccess rewrite rules](http://stackoverflow.com/questions/9153262/tips-for-debugging-htaccess-rewrite-rules) – Masivuye Cokile Feb 16 '17 at 11:21
  • I'm not very experienced with htaccess and after reading through that i still haven't a clue where i'm going wrong, the info looks solid but perhaps lost on me because of my experience, thank you though. – user2171245 Feb 16 '17 at 11:30

1 Answers1

1

No need to escape / in rewrite rules and make leading slash optional as .htaccess files don't match leading /. Also you can greatly reduce capturing groups in your pattern:

RewriteRule ^/?(segment1/segment2/[\w-]+/[\w-]+/[\w-]+/(?![^/]*-mystring)[\w-]+)/([\w-]+)(/.*)?$ /$1-mystring/$2$3 [R=301,L,NC,NE]
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • This almost works thank you, it is repeating the string over and over again in the url though. – user2171245 Feb 16 '17 at 11:47
  • For example i am now getting: https://www.mysite.co.uk/segment1/segment2/segment3/segment4/segment5/segment6-mystring-mystring-mystring-mystring-mystring-mystring-mystring-mystring-mystring-mystring-mystring-mystring-mystring-mystring-mystring-mystring-mystring-mystring-mystring-mystring-mystring-mystring-mystring-mystring-mystring-mystring-mystring-mystring/segment7 – user2171245 Feb 16 '17 at 11:57
  • sorry if it spammed you i just tried to tag you in my comment. The url structure is the exact same however it's loading the page now, previously i was getting a too many redirects error. Just need to trim that url to only show the string once. Thanks! – user2171245 Feb 16 '17 at 12:03
  • Once you clear your browser cache and if you keep this rule at top of your .htaccess this rule works. On my apache it redirected `http://localhost/segment1/segment2/segment3/segment4/segment5/segment6/segment7` to this: `http://localhost/segment1/segment2/segment3/segment4/segment5/segment6-mystring/segment7` – anubhava Feb 16 '17 at 12:08