EliCollins
EliCollins

Reputation: 89

404 errors with .htaccess and php files

I keep getting 404 errors when I type.

localhost/shirt.php?id= followed by an number from an array I created

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]

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

Using MAMP with Chrome and I've cleared my cache multiple times

Upvotes: 0

Views: 117

Answers (2)

EliCollins
EliCollins

Reputation: 89

So after staying up for a couple more hours I've figured it out

Adding a Trailing Slash to the end of the url

RewriteRule ^(contact/[a-zA-Z0-9]+)$ /$1/ [R=301]

Rewriting the Rules with Query Strings

RewriteRule ^contact/([a-zA-Z0-9]+)/$ /contact/index.php?status=thanks
RewriteCond %{QUERY_STRING} ^status=([a-zA-Z0-9]+)$
RewriteRule ^index.php$ /contact/%1/? [R=301]

This is what worked for me

Upvotes: 0

anubhava
anubhava

Reputation: 785481

You can use this code in your DOCUMENT_ROOT/.htaccess file:

Options -MultiViews
RewriteEngine On
RewriteBase /

# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+shirt\.php\?id=([^\s&]+) [NC]
RewriteRule ^ shirt/%1? [R=302,L,NE]

# internal forward from pretty URL to actual one
RewriteRule ^shirt/(\d+)/?$ shirt.php?q=$1 [L,QSA,NC]

Make sure you don't have any other rule when you test this.

Upvotes: 1

Related Questions