mnstrflkrn
mnstrflkrn

Reputation: 503

Multiple RewriteRule

I would like to have multiple RewriteRules in my .htaccess file.

# Enable Rewriting
RewriteEngine on

# Rewrite profile urls
# Input: /user<userId>
# Output: /profile.php?id=<userId>
RewriteRule ^user(\d+)/?$ profile.php?id=$1 [L]

# Rewrite by default to redirect.php
RewriteRule .* redirect.php

Every requests points to redirect.php

I thought, with the [L] flag in the first RewriteRule, would stop processing the rule set.

Upvotes: 1

Views: 2057

Answers (3)

arzzzen
arzzzen

Reputation: 361

[L] flag stops processing only in current iteration but mod_rewrite repeat process until path will not be constant. In your case I think you can use this rule:

RewriteRule ^user\d+$ profile.php

RewriteCond %{REQUEST_URI} !profile.php
RewriteRule ^.*$ redirect.php

Upvotes: 0

mnstrflkrn
mnstrflkrn

Reputation: 503

Adding the following to the second RewriteRule works.

# Rewrite by default to redirect.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* redirect.php

But im not sure, why this is needed.

Upvotes: 0

If you would like to rewrite /user<userId> to /profile.php?id=<userId> and rewrite the other URLs to /redirect.php, then you could try these two configuration directives:

RewriteEngine on

RewriteRule ^user([a-zA-Z0-9_-]+)/?$ /profile.php?id=$1 [L]

RewriteRule .* /redirect.php

OR:

RewriteEngine on

RewriteRule ^user([a-zA-Z0-9_-]+)/?$ /profile.php?id=$1 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* /redirect.php

Upvotes: 1

Related Questions