2

I have tried all i could up to no avail, checked all web tutorials on url .htaccess rewriting, none solved all my problem exclusively, my programming experts help me with this, i have a php web application, the .htaccess code I have only manages url's without the .php extension, like localhost/app/images.php, the link is localhost/app/images, once you click on it, .htaccess understands /images is /images.php and fetches the document, but when i tried to put some more .htaccess code to rewrite some dynamic links like /images/miscellaneous where the right link should be

/images.php?album_id=miscellaneous

i get

internal server error

This is the .htaccess code i have now, it only matches /images to /images.php

# Turn on URL rewriting
RewriteEngine on
RewriteBase /ansjc
# Remove file extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) $1.php [L]
RewriteRule images/album_id/(.*)/ images.php?album_id=$1
RewriteRule images/album_id/(.*) images.php?album_id=$1 

2 Answers 2

1

Your rewrite rules have two major problems:

  • the order of them matters. Right now, your second and third will never match stuff
  • two of them could be simplified into one.

Consider using this:

 RewriteEngine on
 RewriteBase /ansjc
 # Remove file extension
 RewriteRule images/album_id/(.+)/?$ images.php?album_id=$1 [L]
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteRule (.+) $1.php [L]

The [L] flag signifies "last". In other words, if it matches, nothing else will be processed in terms of rewrites. So, if images/album_id/etc/ gets matched, the second rewrite rule will not interfere.

This also solves the issue of the .php being appended to everything. Though I suspect your 500 might be from your code, not from rewrites.

Sign up to request clarification or add additional context in comments.

2 Comments

This solution works but removes the stylesheets, and no data is displayed, i think i'm the one making the mistake, this is how my link starts: echo "<li class='album'><a href='images/$title'> so once the link is clicked, i want .htaccess to pass the variables to the page images.php?album_id=Miscellaneous
@NwachukwuAlNnaemeka: remember that images.php suddenly does not magically understand that it is now two directories down from where it used to be. IF your stylesheets and images are specified relatively (href="css/styles.css"), you'll need to adjust those to be absolute.
0

Use this code

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^([^/]*)$ $1.php [NC,L]
RewriteRule ^images/(.*)$ images.php?album_id=$1 [L]

and try

http://localhost/images
http://localhost/images/
http://localhost/images/album_id

it will call images.php and inside images.php just print_r($_GET); to test.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.