Rewrite (.*)

Can someone pls exaplain why this doesn’t work? RewriteRule (.*) html/pages/$1/index.php [L]

I’m trying to get everthing after “/” and store it in $1
for example would go to

Because it creates an infinite loop.

  1. Rewrite se to html/pages/se/index.php
  2. Rewrite html/pages/se/index.php to html/pages/html/pages/se/index.php/index.php
  3. Rewrite html/pages/html/pages/se/index.php/index.php to html/pages/html/pages/html/pages/se/index.php/index.php/index.php

And so on and so forth.

What you need is a RewriteCond that informs Apache not to rewrite when the URL already starts with html/pages.


Thanks for your reply. But why does it continue? What makes it continue?

I find many explanations where it is mentioned that the loop is the cause, but not what causes the loop.

  1. Rewrite se to html/pages/se/index.php

This is good , this is correct. Why is it not done here? What in the regex makes it continue?

A rewrite redirects from one URL to another.
When you go to the initial URL the rules in .htaccess are followed and you are redirected, as per your rule.
As you request the new URL the rules in the .htaccess are followed as with any URL, you are redirected again.
And again…
Any redirect means that .htaccess is run again and any rules in it followed again.
Because your rule redirects any URL, every URL redirects and falls into a loop.

Any URL is passed through all RewriteRules in the .htaccess file.
If the URL that comes out is different than the URL that goes in then Apache starts the process over.
This continues until the URL that goes in is the same as the URL that comes out, or until Apache has looped 10 times, whichever comes first.

So it’s not the regex that loops, it’s Apache that loops.

Not entirely correct, Apache starts to use the new URL internally for the request. Without the [R] flag on a RewriteRule Apache won’t actually redirect the user.

1 Like

Based on your answers, then why doesn’t this (or similar) continue over and over again?
RewriteRule start html/pages/start.php

Why are you even structuring like in the first place?

It’s not about that, i’m trying to understand how the rewrite works

What is the entire contents of your .htaccess?

Because it doesn’t change over and over again. First it changes from whatever it was that contains start to html/pages/start.php, but then when a next round is started it is “changed” to html/pages/start.php, but since that’s the same value as we started with, it didn’t actually change and Apache doesn’t count it as a change.

You may want to look into some documentation:

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.