Insyte
Insyte

Reputation: 2236

.htaccess RewriteRule conditionals?

I'm trying to create two RewriteRules but cannot seem to get mod_rewrite to play nice.

What I am trying to achieve is the following...

domain.com/my/friendly/url = domain.com/index.php?a=$1

This one is simple enough, but I am trying to check for the existence of a number (of arbitrary length) to then add as a second request parameter.

For example

domain.com/my/friendly/url/100 = domain.com/index.php?a=$1&id=$2

Where $1 is my/friendly/url and $2 is 100.

I've tried numerous patterns without success. My .htaccess file is left looking like this and is failing.

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)(\/[0-9]+)$ index.php?a=$1&id=$2 [L,QSA]
RewriteRule ^(.*)$ index.php?a=$1 [L,QSA]

Upvotes: 1

Views: 43

Answers (1)

anubhava
anubhava

Reputation: 786041

You can have your rules like this:

# ignore all rules for real files or directories
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

RewriteRule ^(.+?)/([0-9]+)/?$ index.php?a=$1&id=$2 [L,QSA]

RewriteRule ^(.+)$ index.php?a=$1 [L,QSA]

Upvotes: 1

Related Questions