htaccess rewrite rule ends in an infinite loop


I would like to permanently redirect

www.example.com/event/title-goes-here to www.example.com/event/jazz/title-goes-here

with a regex in my htaccess file.

I currently have:

RewriteEngine on RewriteRule ^event/(.*)$ http://www.example.com/event/jazz/$1 [R=301,L]

But this results in an infinite loop (it goes to example.com/event/jazz/jazz/jazz/jazz etc) because the /event/ part is the same.

How can I use the rewrite rule to make this rewrite, and also in a way that it takes into account that people can go to example.com/event/title-goes-here AND example.com/event/title-goes-here/ (so with a trailing slash) while still correctly making the redirect.


Based on anubhava's answer my complete .htaccess file now looks like this (forgot to mention there's also a rewrite on index.php):

RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule .* index.php/$0 [PT,L] RewriteRule ^event/([^/]+)/?$ /event/jazz/$1 [R=301,L,NC,NE]

Also forgot to mention I'm using CodeIgniter and in my routing.php there are routing options listed below (don't know if this matters). Thought this was purely a .htaccess thing but when I thought about the routing file I figured that maybe this messes with the rewriting?

$route['event/jazz/(:any)'] = 'event/view/$1'; $route['(:any)'] = 'pages/view/$1';


Yes it will loop because source and target URIs both match your regex pattern.

Use this rule to fix it:

RewriteEngine on RewriteRule ^event/([^/]+)/?$ /event/jazz/$1 [R=301,L,NC,NE] RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule .* index.php/$0 [PT,L]


  • Replacing contents inside curly braces (for e.g. {1}) with something else [duplicate]
  • 301 redirect with complex directory regex
  • make m4 see macro when macro ends with same character as string following macro
  • Strip down any parameter from URL which is not whitelisted
  • Can I use preg_replace to strip the trailing \\n in multiline mode?
  • auto layout leading or trainling -16
  • Custom URI routing by query string with CodeIgniter?
  • Why does my regular expression fail with certain substitutions?
  • Why does _GET in PHP wrongly decodes slash?
  • help('modules') crashing? Not sure how to fix
  • Zeromq with python hangs if connecting to invalid socket
  • Refactoring advice: maps to POJOs
  • Generating anchors with PyYAML.dump()?
  • Haskell: function composition with anonymous/lambda function
  • Regex for URL rewrite with optional query string parameters
  • F#: In which memory area is the continuation stored: stack or heap?
  • Configure Spring's MappingJacksonHttpMessageConverter
  • Azure webjobs output logs indexing taking very long
  • How to autopopulate a field in SugarCRM form
  • Multicolored edittext hint
  • converting text file into xml using php?
  • Redux Form - Not able to type anything in input
  • Apache RewriteRule redirection with url encoded
  • Zurb Foundation _global.scss meta styles for js?
  • ADO and msqli connections very slow
  • Marklogic : Query response time is very high
  • How can I sort a a table with VBA with given text condition?
  • Installing iPhone App to iPhone
  • Abort upload large uploads after reading headers
  • Use of this Javascript
  • How to use remove-erase idiom for removing empty vectors in a vector?
  • All Classes Conforming to Protocol Inherit Default Implementation
  • Java Scanner input dilemma. Automatically inputs without allowing user to type
  • Jquery - Jquery Wysiwyg return html as a string
  • Arrays break string types in Julia
  • Data Validation Drop Down Box Arrow Disappearing
  • WPF Applying a trigger on binding failure
  • CSS Applying specific rule for a specific monitor resolution with only CSS is posible?
  • Java static initializers and reflection
  • Can Visual Studio XAML designer handle font family names with spaces as a resource?