0

I used a stackoverflow answer to come up with what I was trying to do. I want requests to http://myserver.com/book/1234 to actually show the content form php file http://myserver.com/book.php?id=1234

So my .htaccess looks like this:

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /

RewriteCond %{QUERY_STRING} ^id=([0-9]*)$
RewriteRule ^book/(.*)$ /book.php?id=$1 [L]

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]

Notice the bottom two lines allow me to use url's like http://myserver.php/login to actually pull up http://myserver.php/login.php This is working correctly. But I get a 500 Error when I try calling http://myserver.com/book.php?id=1234 What is wrong?

  • You started your question by saying that you want requests to myserver.com/book/1234 to actually show the content form php file myserver.com/book.php?id=1234 but then you ended by stating the URL http://myserver.com/book.php?id=1234 doesnt work. Does http://myserver.com/book/1234 work for you? and can you clarify your question? – the_velour_fog Jan 28 '16 at 5:23
1

Your question seems to have conflicting statements, but assuming this statement

I want requests to http://myserver.com/book/1234 to actually show the content form php file http://myserver.com/book.php?id=1234

is what you want.
If you request http://myserver.com/book.php?id=1234 - then your server will flip out based on your current .htaccess. The reason is you should only be performing this directive

RewriteRule ^book/(.*)$ /book.php?id=$1 [L]

Under certain conditions, e.g. if the URL is not for a real file or directory.
This directive

RewriteCond %{QUERY_STRING} ^id=([0-9]*)$

Is telling apache not only go ahead and do the rewrite when a request has the form

some_URI?id=some_number

but in fact the rewrite will ONLY occur when the request has that form.
When a request is for http://myserver.com/book.php?id=1234 you can prevent the rewrite occurring with

RewriteCond %{REQUEST_FILENAME} !-f

So then you .htaccess should look like

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^book/(.*)$ /book.php?id=$1 [L]

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
|improve this answer|||||
  • Awesome. That did the trick. – Thread7 Jan 28 '16 at 5:56
0

I just tested this locally, and it works. Make sure your apache.conf allows the overrides you have specified in your .htaccess file:

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_URI} ^\/book\/([0-9]+)$
RewriteRule ^ /book.php?id=%1 [R,L]

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
|improve this answer|||||

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.