Apache mod_rewrite: how to handle slash in URL

apache-2.2mod-rewrite

I have a following URL structure:

http://mysite.com/movies/index.php?id=123

I want to get rid of index.php?id=123 and instead use the following URL:

http://mysite.com/movies/123

I came up with the following .htaccess in /var/www/mysitecom/movies directory:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ mysitecom/movies/index.php?id=$1 [L,QSA]

And it works. For example, when I specify …

http://mysite.com/movies/123

… I get …

http://mysite.com/movies/index.php?id=123

Troubles begin when I specify …

http://mysite.com/movies/123/

With this slash, I don't have URL handled properly. It looks like I didn't specify id at all, and all my css styles are not applied.

I tried to play with regular expressions in the rule like …

RewriteRule ^([^/]+)[/]*$ mysitecom/movies/index.php?id=$1 [L,QSA]

… but it didn't help.

So my question is how to handle slash in my case? Should I choose my rewrite rule or need to do something else? I need to ensure that http://mysite.com/movies/123/ handles exactly in the same way as http://mysite.com/movies/123. Any thoughts?

Thanks,

Update:

User DSumsky helped me (below).

Dsumsky, thank you very much for your answer. I decided to comment in the form of the answer to get ability of formatting the text.

First of all, I failed to enable logging. With this strings in my .htaccess …

RewriteLog "/var/log/apache2/rewrite.log"
RewriteLogLevel 3

… I have the following error:

Rewrite log not allowed here

Well, I placed these strings into /etc/apache2/apache2.conf and restarted apache. The rewrite.log was created, but it gets empty even when rewrite happens.

Second, it didn't work at all without the following strings:

RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f

So I added them. Then, something went wrong with %{HTTP_HOST}, so I changed it a little by specifying a path:

RewriteRule ^([^/]*)/?$ mysitecom/movies/index.php?id=$1 [L]

Things got better. With both http://mysite.com/movies/123 and http://mysite.com/movies/123/ I see id applied. In the former case everything is completely good, but in the later case css styles are not applied as though they are not specified at all. I don't understand why it happens. For me, it looks like styles are applied BEFORE Rewrite Rule is being executed. I think so because I link styles in the following way:

So, it looks like for /123 it works, I mean Apache is able to find ../library/main.css, but for /123/ it seems to be an embedded directory and ../library/main.css doesn't work.

Any ideas?

Update2:

I got it. The final .htaccess:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RedirectMatch ^(.+[\d+])/$ $1
RewriteRule ^([^/]*)$ mysitecom/movies/index.php?id=$1 [L]

DSumsky, thanks for your help! I mark your post as an answer.

Best Answer

I created a testing VirtualHost to meet your requirements and configured the following rewrite rule which seems to do what you intended:

RewriteEngine On
RewriteLog "/var/log/httpd/rewrite.log"
RewriteLogLevel 3

RewriteCond %{HTTP_HOST} mysite\.com [NC]
RewriteRule ^/movies/([^/]*)/?$ %{HTTP_HOST}/movies/index.php?id=$1 [L]

The rule matches the last portion of URI which is a kind of movie ID and strips off trailing slash if it's there.

When you are not sure what's going on regarding rewrite engine enable logging as I did configured. You can inspect every singles step the engine performed then and test your rules properly.

I tested it with the following URLs:

http://mysite.com/movies/123
http://mysite.com/movies/123/

I found the corresponding lines in the log:

init rewrite engine with requested uri /movies/123
applying pattern '^/movies/([^/]*)/?$' to uri '/movies/123'
rewrite '/movies/123' -> 'mysite.com/movies/index.php?id=123'

init rewrite engine with requested uri /movies/123/
applying pattern '^/movies/([^/]*)/?$' to uri '/movies/123/'
rewrite '/movies/123/' -> 'mysite.com/movies/index.php?id=123'
Related Topic