1
votes

My .htaccess file:

RewriteEngine On

RewriteRule ^([a-zA-Z0-9_-]+)$ photo.php?id=$1

RewriteRule ^([a-zA-Z0-9_-]+)/$ photo.php?id=$1

My goal is to convert the url mysite.com/photo.php?id=31 to mysite.com/31. But when i visit mysite.com/31, all i get is:

Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator, admin@localhost and inform them of the time the error occurred, and anything you might have done that may have caused the error.

More information about this error may be available in the server error log.

Im running it on a wamp server.

Edit:

My current url looks like this: http://localhost:8080/img/photo.php?id=1. Do i need to add the /img-part to htaccess?

2
Where is this htaccess file, in your document root or inside the /img/ directory? Do you have other rules elsewhere? - Jon Lin
@JonLin its in the /img/ folder. And no other rules elsewhere - Johan
So you have this htaccess file in you /img/ directory but you want rewrites to happen in your document root (e.g. mysite.com/31)? - Jon Lin
@JonLin The root in this case would be /img/. The url is a bit missleading - Johan
Or is it that you actually want to rewrite URL's like: mysite.com/img/31? - Jon Lin

2 Answers

1
votes

If you want to be able to access images via the URL http://mysite.com/31, note, there is no /img/ directory in the URL, you need to put your rewrite rules in an htaccess file in your document root. The directory that the img directory is in. Since I'm also going to assume you've got other stuff on your site besides images, you're going to want to check that you are rewriting requests that aren't for images, and you can do that by checking against %{REQUEST_FILENAME} and the !-f and !-d flags.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_-]+)/?$ /img/photo.php?id=$1 [L]

Also, since your existing rules are just fine if you are trying to access your images via URLs like: http://mysite.com/img/31, there is something else that is causing your 500 server error. Please make sure that the rewrite module is loaded. In WAMP, right click on the WAMP icon and go to apache, then apache module and make sure rewrite module is checked.

1
votes

Try this

<IfModule mod_rewrite.c>    
    Options +FollowSymLinks    
    RewriteEngine On

    RewriteRule ^([a-zA-Z0-9_\-]+)$ photo.php?id=$1    
</IfModule>