0

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>
        ServerAdmin morrison.brendan17@gmail.com
        ServerName mind-knowledge.com
        ServerAlias www.mind-knowledge.com

        DocumentRoot /var/www/tagmenu
        <Directory />
                Options FollowSymLinks
                AllowOverride All
        </Directory>
        <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
                </IfModule>
        </Directory>
</VirtualHost>

now from what I understand the rewrite rule should allow http://www.mind-knowledge.com/PlayVideo/13424/ to be rendered as the page http://www.mind-knowledge.com/PlayVideo/index.php?videoID=13424 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

2 Answers2

3

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.

Ladadadada
  • 25,847
  • 7
  • 57
  • 90
  • 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 `` tag but rather just inside the `` 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 `` context because it's always in a directory. – Ladadadada Jan 25 '14 at 00:53
  • Could you help answer a similar question: https://stackoverflow.com/questions/58853639/unable-to-forward-the-query-parameters-submitted-by-the-user?noredirect=1#comment103999680_58853639 @Ladadadada – Amanda Nov 15 '19 at 03:13
0

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.

user204992
  • 11
  • 1