Dino Prašo
Dino Prašo

Reputation: 611

Mod_rewrite, check if string is empty

I need to write a .htaccess file to rewrite all urls which have anyting after the domain.

For example RewriteRule ^(.*)$ index.php?page=course&course=$1 [L]

But this sends example.com to that rewritten url as well, and I don't want that. I want it to rewrtire it ONLY if these is really something after the domain, like example.com/CITA180.

I know that I could do example.com/c/CITA180 and then do RewriteRule ^c/(.*)$ index.php?page=course&course=$1 [L] but I don't want to do it like that if I don't have to.

Thanks.

Upvotes: 1

Views: 558

Answers (2)

Rishabh
Rishabh

Reputation: 1

Add regex condition: RewriteCond %{REQUEST_FILENAME} REGEX

In your case: RewriteCond %{REQUEST_FILENAME} [A-Z]
This will check if it contains any character or not.

Along with not a file check: RewriteCond %{REQUEST_FILENAME} !-f

Upvotes: 0

anubhava
anubhava

Reputation: 785406

You can use .+ instead of .* to make sure it doesn't match landing page. You will also need RewriteCond %{REQUEST_FILENAME} !-f condition to avoid matching default landing page:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?page=course&course=$1 [L]

Upvotes: 2

Related Questions