I have a following URL structure:


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


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 ...


... I get ...


Troubles begin when I specify ...


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?



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?


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.

1 Answer 1


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:


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'
  • Dsumsky, thank you very much for your answer. I marked your answer as useful, and it is very useful indeed. I have some kind of progress, but not 100%. I explained the current state below as an answer to keep formatting. Any ideas?
    – user54614
    Sep 5, 2013 at 19:27
  • Update: my post was deleted, so I updated the question above. Thanks!
    – user54614
    Sep 5, 2013 at 20:13
  • Better yet, if you know that id must be numeric, then look for ^/movies/(\d*) — you might as well try to avoid some attack vectors while you rewrite. Sep 6, 2013 at 7:35
  • RewriteLog option has been deleted in Apache 2.4. Instead append rewrite:trace3 to LogLevel. For example: LogLevel info rewrite:trace3
    – rustyx
    Apr 20, 2015 at 13:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.