PHP Using PHP with MySQL Connecting PHP to MySQL Introducing Databases

Jesse Gravenor
Jesse Gravenor
27,272 Points

Shirts directory files not working with .htaccess and clean URLs

I was not able to get the shirts page, nor the shirt detail page links to work with the download files.

The .htaccess Rewrite lines seemed to be working only to point the browser to the shirts/ directory, but stop short of loading the shirts.php or shirt.php file. In fact I would land on the shirts/ directory page with the files listed. I could follow the link to the shirts.php, but could not link to the shirt detail page through the shirt.php.

I was able to re-dirty the URLs on the shirts detail page and change the shirts.php to an index.php; and this is working to go forward with this course. I also had to remove the offending lines from the .htaccess folder dealing with shirts/, shirts.php and shirt.php.

I spent a good amount of time getting aquanited with .htaccess files, but sot nowhere in the end. If anyone else encountered this or found a solution, please let me know!

Here is the .htaccess file. It is the same as from the download, but with subdirectory/ added where necessary:

RewriteEngine On

RewriteRule ^subdirectory/shirts/$ mikesshirtssql/shirts/shirts.php

RewriteRule ^subdirectory/shirts/([0-9]+)/$ shirts/shirt.php?id=$1

RewriteRule ^receipt.php$ /receipt/ [R=301]

RewriteRule ^contact.php$ /contact/ [R=301]

RewriteRule ^shirts.php$ /shirts/ [R=301]

RewriteRule ^(shirts/[0-9]+)$ /$1/ [R=301]

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

RewriteRule ^shirt.php$ /shirts/%1/? [R=301]

Again, what happens is that when you link or navigate to localhost/subdirectory/shirts/ you are taken to the geneic directory page displaying links to shirts.php and shirt.php. This file is supposed to direct to the shirt.php file, without having to change the name to index.php. The larger problem is the shirt query strings don't get directed to shirt.php, as is also the intent of this file.

Robert Mews
Robert Mews
11,540 Points

Hey Jesse,

Try removing the backslashes on the shirts files, like this

RewriteEngine On
RewriteRule ^shirts/$ shirts/shirts.php
RewriteRule ^shirts/([0-9]+)/$ shirts/shirt.php?id=$1
RewriteRule ^receipt.php$ /receipt/ [R=301]
RewriteRule ^contact.php$ /contact/ [R=301]
RewriteRule ^shirts.php$ shirts/ [R=301]
RewriteRule ^(shirts/[0-9]+)$ /$1/ [R=301]

RewriteCond %{QUERY_STRING} ^id=([0-9]+)$
RewriteRule ^shirt.php$ shirts/%1/? [R=301]

EDIT: You don't remove the backslash on the receipt and contact files since those aren't contained in a directory (i.e. the shirt files are located in a separate directory).

3 Answers

Tom Cawthorn
Tom Cawthorn
22,982 Points

Can you post the .htaccess file you tried with?

Check the Markdown Cheatsheet if you're wanting to format it ;)

you might want to check if redirects are turned on in your apache server setup i know i had to turn mine on.

Jesse Gravenor
Jesse Gravenor
27,272 Points

Shawn,

Thanks. It seems to be turned on because before I added the subdirectory it was rewriting shirts/ as if it were in the root.

Jesse Gravenor
Jesse Gravenor
27,272 Points

Thank you Robert. That did it!