Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

hey SO I am attempting to use apache's mod_rewrite module to do some basic rewrting of url on my website so they become a little more seo friendly

basically I have a virtual host file that look like this

    <VirtualHost *:80>

        DocumentRoot /var/www/tagmenu
        <Directory />
                Options FollowSymLinks
                AllowOverride All
        <Directory /var/www/tagmenu>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
                Order allow,deny
                allow from all
                <IfModule mod_rewrite.c>
                        RewriteEngine on
                        RewriteRule /video/([0-9]+) /PlayVideo/index.php?videoID=13424

now from what I understand the rewrite rule should allow to be rendered as the page is this correct?

As currently I get 404 , and i have run sudo a2enmod rewrite and reloaded apache.

Can anyone help me in what I am doing wrong or suggest some ways to troubleshoot the problem thanks in advance. brendan

share|improve this question

marked as duplicate by EEAA, Falcon Momot, mdpc, Jenny D, Ladadadada Jan 28 '14 at 20:11

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

@EEAA not entirely a duplicate although this is a handy reference it doesnt quite answer my question on why my rule is not working – brendan morrison Jan 24 '14 at 21:49
@Mxx same person same question – brendan morrison Jan 24 '14 at 22:48
up vote 3 down vote accepted

You want /PlayVideo/13424/ to work but the first part of your rewrite rule is /video/.

You also don't use the numbers matched in the first part in the second part, meaning that everyone will always watch the same video, no matter what the number in their URL.

Change it to this to fix both those problems:

RewriteRule /PlayVideo/([0-9]+) /PlayVideo/index.php?videoID=$1

I should have noticed this before but there are potentially two other issues. The first is that your rewrite rules are inside a <Directory> block and this changes how they work. The simple way of explaining this is that the leading slash is removed.

The more technically accurate description is that the path that is passed in to the rewrite rule to be matched is a filesystem path rather than a URI and it has the directory removed from the start of it.

So inside a <Directory> block, you should use:

RewriteRule PlayVideo/([0-9]+) PlayVideo/index.php?videoID=$1

The other thing is minor but I prefer not to have an <IfModule> block at all. These are handy for package maintainers whose configs are going to be deployed in thousands of different machines by people of all competence levels but for a single deployment with (probably) a single admin, all they do is hide other mistakes. If you forgot to load the rewrite module (commented out the line, deleted the symlink, whatever) Apache will still start up just fine and throw no errors but the rerwrite module won't work and URIs that rely on it will result in a 404, just like you're seeing. Without the <IfModule> block, that same mistake would cause Apache to fail to start up and would result in a useful error message.

share|improve this answer
hey sorry about this ... although you are correct that i want PlayVideo I have actually been testing with video. as for the static number this was just for testing . However using the rule you have scpecified i still get a 404 any idea how to debug this – brendan morrison Jan 24 '14 at 22:10
Oh yes, I can see another issue. Updated the answer. – Ladadadada Jan 24 '14 at 22:54
ahh okay thank you it works! now just to clarrify if the block was not inside the <Directory> tag but rather just inside the <VirtualHost> tag I would need the / before the PlayVideo and the rule would work an all directories not just /var/www/tagmenu is this correct? – brendan morrison Jan 24 '14 at 23:08
Yes, that's correct. Also, in case it bites you in the future, a .htaccess file is implicitly in a <Directory> context because it's always in a directory. – Ladadadada Jan 25 '14 at 0:53

While its possible to match that way, My brain doesn't do regex well so I tend to split things up more. While, it is slower on the apache side, I prefer something more along the lines of:

RewriteBase /PlayVideo
RewriteCond %{REQUEST_URI} ^/([0-9]+)$
RewriteRule ^(.*) index.php?video=$1 [L,QSA]

QSA is not neccessary but you may wish to pass more query strings in the future. This gives you that ability.
Also if you have access to the logs, set the following (while debugging only!): RewriteLogLevel 4

Then step through the logs to see at what point did the rewrite rule do something different than what you expected.

share|improve this answer

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