Multiple rewrite conditions and rules

This is the first that that I worked with the htaccess file because I wanted to keep the easy way of GET variables and make the url nice. So I have the following code in my .htaccess file.

So the following URL: http://www.rsteams.com/act/test should ‘direct’ to http://rsteams.com/index.php?act=test

When not using the second condition which does the GET stuff, it works (remove the www). What am I doing wrong?


RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.rsteams.com [NC]
RewriteRule ^(.*)$ http://rsteams.com/$1 [L,R=301]

RewriteCond %{REQUEST_URI} act/(.*)/
RewriteRule ^act/(.*)/$ http://rsteams.com/index.php?act=$2

AuthGroupFile /dev/null
AuthType Basic
AuthUserFile /home/*CENSORED*/domains/rsteams.com/.htpasswd/public_html/.htpasswd
AuthName "Administrators' Area"
require valid-user

<FilesMatch "\\.php$">

AddHandler x-httpd-php53 .php

</FilesMatch>

The FilesMatch I had to setup to use PHP 5.3 on my webhost that I rent.

Dreeass,

You’re using the LAZY code to capture the entire {REQUEST_URI} and not the parts. How do you expect (.*) to be translated to act= … well, not test but act/test? Moreover, WHY the trailing / in your RewriteRule? Then, too, what do you think RewriteBase / is doing? The second RewriteCond statement? :nono:

Regards,

DK

I’m just following a tutorial from YouTube and some stuff I read about this, there’s no need to be angry. Well explain to me in normal terms what I’m doing wrong and if you’d be so kind, please show it to me in code so I understand. But don’t just give me code if you want, I’d like to understand it too so I can use it in the future.

Also the RewriteBase and the part to remove www wasn’t written by me but by someone from the webhosting service.


RewriteEngine On

This is fine. Without this the rest wouldn’t work.


RewriteBase /

Everybody and their aunt puts this in the their .htaccess while in 99 out of 100 cases it’s not needed at all. The only time I recall I’ve ever needed it is on a host with mass virtual hosting, but I don’t think many hosts in the wild use this.
Most likely you can remove this without any consequences.


RewriteCond %{HTTP_HOST} ^www.rsteams.com [NC]
RewriteRule ^(.*)$ http://rsteams.com/$1 [L,R=301]

Other than forgetting to escape the dots in the RewriteCond (i.e. it should be www\.rsteams\.com) this is okay. Slightly better would be


RewriteCond %{HTTP_HOST} ^www\\.rsteams\\.com [NC]
RewriteRule .? http://rsteams.com%{REQUEST_URI} [L,R=301]

because it doesn’t introduce a new variable, but the difference in performance is negligible.


RewriteCond %{REQUEST_URI} act/(.*)/

This line serves no purpose whatsoever, since this intent is already conveyed in the next line. Get rid of this line.


RewriteRule ^act/(.*)/$ http://rsteams.com/index.php?act=$2

Remove the trailing / at the end (as per your spec, you wanted to match /act/test – no trailing slash there), and replace (.*) with the characters you actually want to match. Also, you should just put index.php there, not the complete URL – as that might trip Apache into a 302 redirect instead of a just a rewrite. Also, you’re referencing $1, not $2 (since it’s the first atom – the first match within parentheses). There is no $2 (or above) in this RewriteRule.
For example, to match digits and letters, it’d be:


RewriteRule ^act/([a-z0-9]+)$ index.php?act=$1


AuthGroupFile /dev/null
AuthType Basic
AuthUserFile /home/*CENSORED*/domains/rsteams.com/.htpasswd/public_html/.htpasswd
AuthName "Administrators' Area"
require valid-user

This is fine


<FilesMatch "\\.php$">
AddHandler x-httpd-php53 .php
</FilesMatch>

Not a fan of this setup but as this is completely out of your hands: fine as is.

All together now:


RewriteEngine On

RewriteCond %{HTTP_HOST} ^www\\.rsteams\\.com [NC]
RewriteRule .? http://rsteams.com%{REQUEST_URI} [L,R=301]

RewriteRule ^act/([a-z0-9]+)$ index.php?act=$1

AuthGroupFile /dev/null
AuthType Basic
AuthUserFile /home/*CENSORED*/domains/rsteams.com/.htpasswd/public_html/.htpasswd
AuthName "Administrators' Area"
require valid-user

<FilesMatch "\\.php$">
AddHandler x-httpd-php53 .php
</FilesMatch>

Thank you very much for the explanation, it directs well now. Thanks a ton!
(Bedankt)

Nice job, Rémon!

Regards,

DK