0

I tried to get the Regex from this working:
mod_rewrite: replace underscores with dashes

But it leads to an endless loop for me and in case if only one underscore is in the URL it creates a redirect beginning at C: ...:
Something like:

http://localhost:7000/C://whatevercomesnext

Heres the Rule:

RewriteCond %{THE_REQUEST} _ 
RewriteRule ^(?news/.*/.*/[^/]*?)_([^/]*?_[^/]*)$ $1-$2 [N=10]
RewriteRule ^(?news/.*/.*/[^/]*?)_([^/_]*)$  $1-$2 [R=301,L]

It should convert urls like this:
http://localhost:.../news/2019/03/vollautomatisierte_abwicklung_eines_service_incidents_bei_retailer

Into this:
http://localhost:.../news/2019/03/vollautomatisierte-abwicklung-eines-service-incidents-bei-retailer

It's always the part after the last / and only if there is the news part which hast to be converted. Nothing else needed, in fact all the other stuff should stay untouched.

Seems like removing those underscores is a very difficult problem since I found many solutions but no rule which works in general.

Now im totally confused. I testet my Rules via the htacces tester website: https://htaccess.madewithlove.be?share=7a4c3743-2f31-5489-aafe-1eebe41432ed

It worked only with a minor change in the RewriteCond.

The Tester just produced what i expected.

But when I test it locally the produced URL is a absolute mess. It generates URL is the base URL concatenated multiple times.

Probably for every single loop one time the complete URL....

No clue what to search for or how to fix this now.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
delareon
  • 1
  • 2
  • It looks like your pattern is not valid to begin with, https://regex101.com/ says the first part, `(?news/.*/.*/[^/]*?)`, has an “Incomplete group structure”. – CBroe Jul 23 '20 at 07:58
  • Yeah i found this never worked on any test site but doesnt make any difference if i delete those leading ? – delareon Jul 23 '20 at 08:19
  • Also on the testsites they always complain about not escaping but in the htaccess it leads to errors if you excape.... – delareon Jul 23 '20 at 08:24
  • Do not use `[R=301]` flag until you're sure about the rules. 301 leads to browser caching the redirection results. While testing, use `R=302`. when satisfied, replace with `301`. – hjpotter92 Jul 27 '20 at 04:48
  • Does this answer your question? [301 Redirect to replace all spaces to hyphens](https://stackoverflow.com/questions/5821120/301-redirect-to-replace-all-spaces-to-hyphens) – hjpotter92 Jul 27 '20 at 05:02

0 Answers0