Apache, mod_rewrite and me. What am I doing wrong?
-
So I'm creating a very very simple API for an app to consume. One basic endpoint and only a single verb.
The API is
GET /schedule/yearreturns a json package. Implementing this works, if I call the script directly (the php script expects /schedule.php?year=$year).
So I'm trying to be all fancy about this and use mod_rewrite to change the fancy /schedule/year into what the script expects. And it's 404'ing.
Here's what I've put in the .htaccess file (which the hosting provider says has mod_rewrite turned on):
RewriteEngine On RewriteRule "^/schedule/(.+)" "/schedule.php?year=$1"
Does this do the right thing? Or is my syntax screwed up? Help? Abuse? Vile imprecations about my mother (who is a fantastic lady, by the way)?
-
@Benjamin-Hall said in Apache, mod_rewrite and me. What am I doing wrong?:
Abuse?
Shut your festering gob, you tit! Your type really makes me puke, you vacuous, coffee-nosed, malodorous pervert!!!
-
Figured it out: turns out I needed to change
RewriteRule "^/schedule/(.+)" "/schedule.php?year=$1"
to
RewriteRule "^schedule/(.+)" "schedule.php?year=$1"
that is, without the initial slashes. Bah.
-
@Benjamin-Hall
I'm so glad I don't have to deal withmod_rewrite
anymore. It's really powerful, but configuring it correctly is always a pain, there are simply too many pitfalls.
-
@asdf said in Apache, mod_rewrite and me. What am I doing wrong?:
@Benjamin-Hall
I'm so glad I don't have to deal withmod_rewrite
anymore. It's really powerful, but configuring it correctly is always a pain, there are simply too many pitfalls.I used to think the same thing until I learned about logging mod_rewrite. I haven't used Apache in a long time, though, so I'm not sure how well that feature's kept up since I last used it. And, yes, despite the logging feature, I don't miss any part of it.
-
@The_Quiet_One said in Apache, mod_rewrite and me. What am I doing wrong?:
I used to think the same thing until I learned about logging mod_rewrite.
Most shared hosting providers won't let you access that log, though. And as we all know, there are occasionally huge differences between different Apache versions. If your local test server has a different one installed, you'll have "fun" figuring out how to adapt the
.htaccess
to get it to work in production.I really don't miss web development...
-
@asdf I remember back when I used to work on an open-source PHP package that supported 'pretty' URLs, the mod_rewrite rule basically sent all URLs to a single PHP script entry point without arguments. We then inspected some
$_SERVER
key to get at the original un-rewritten URL and parsed that in PHP code.
-
@Benjamin-Hall said in Apache, mod_rewrite and me. What am I doing wrong?:
Figured it out: turns out I needed to change
RewriteRule "^/schedule/(.+)" "/schedule.php?year=$1"
to
RewriteRule "^schedule/(.+)" "schedule.php?year=$1"
that is, without the initial slashes. Bah.
Holy shit, kept staring at the two lines for good 30 seconds until I saw your comment and understood what happened.
You should get a medal or something.
-
@kt_ said in Apache, mod_rewrite and me. What am I doing wrong?:
@Benjamin-Hall said in Apache, mod_rewrite and me. What am I doing wrong?:
Figured it out: turns out I needed to change
RewriteRule "^/schedule/(.+)" "/schedule.php?year=$1"
to
RewriteRule "^schedule/(.+)" "schedule.php?year=$1"
that is, without the initial slashes. Bah.
Holy shit, kept staring at the two lines for good 30 seconds until I saw your comment and understood what happened.
You should get a medal or something.
I think I understand why the change is needed, but still, that was obnoxious to find.
-
@Benjamin-Hall said in Apache, mod_rewrite and me. What am I doing wrong?:
my mother (who is a fantastic lady, by the way)?
We know.
Well, youdid open up for it, didn't you?
-
@Benjamin-Hall said in Apache, mod_rewrite and me. What am I doing wrong?:
I think I understand why the change is needed, but still, that was obnoxious to find.
There's plenty of conflicting information about the leading slash and how it relates to Apache versions, the virtualhost/directory/htaccess context it's used in, etc.
After tripping over it a few times, I decided that the easiest approach is to make it optional:
RewriteRule ^/?schedule/(.+) ...
-
@DCoder said in Apache, mod_rewrite and me. What am I doing wrong?:
@Benjamin-Hall said in Apache, mod_rewrite and me. What am I doing wrong?:
I think I understand why the change is needed, but still, that was obnoxious to find.
There's plenty of conflicting information about the leading slash and how it relates to Apache versions, the virtualhost/directory/htaccess context it's used in, etc.
After tripping over it a few times, I decided that the easiest approach is to make it optional:
RewriteRule ^/?schedule/(.+) ...
Thanks. I'll keep that in mind for the future.