I'm migrating a website from an old server thats being shutdown to a new one, and in the process I'm having some trouble with Apache and mod_rewrite. I have the following .htaccess file:
RewriteEngine on
RewriteRule ^audio/(.*?)/.*?$ audio.php?id=$1
RewriteRule ^books/(.*?)/.*?$ books.php?id=$1
The audio.php and books.php are being called as expected except that the captured variables are not being passed, so in the case of the audio.php rewriterule I am being served the audio.php page but $_GET
is empty.
To test what is happening I added RewriteRule (.*) test.php?a=$1
where test.php just dumped $_GET
when I did that $_GET['a']
contained 'test.php'.
Nothing is showing up in the error log, and as the correct files are being served clearly the rewrite is mostly working, and the config has AllowOverride All
what am I doing wrong?
I want to be clear that this htaccess file worked as is on the old server.
You should setup a
RewriteLog
with a highRewriteLogLevel
to ensure your rules are being processed as you expect.Keep in mind that
.*?
may yield an empty match, because*
means 0 or more. You might at the very least want to use+
(so,.+?
), which means one or more.Anyhoo. There are better ways to express what you're looking for.
To match until the next slash, this should do:
Although, if it's a numeric id, you can be more precise:
This is really going to depends on exactly what you are after doing. What path is passed to the original request? Is it always /audio/some_id/other_stuff? What other config is there for paths with "audio" in? For a request of the form:
you will probably want a RewriteRule of:
I think the .*? are not doing what you want. Give that a whirl, and see what it does. You may also find the Perl regular expression reference guide helpful.