1

I have a number of PHP pages with query strings in the URLs on my site. I'm trying to change the URLs for SEO, to do this I'm using an .htaccess file.

I'm new to .htaccess so this is fairly basic and I'm not sure if I'm doing this right or if there is a better way to do it.

My .htaccess file:

RewriteEngine on

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

RewriteCond %{REQUEST_URI} !/.css$
RewriteCond %{REQUEST_URI} !/.js$
RewriteCond %{REQUEST_URI} !/.png$
RewriteCond %{REQUEST_URI} !/.jpg$

RewriteRule ^category/([0-9a-zA-Z]+) category.php?id=$1
RewriteRule ^product/([0-9a-zA-Z]+) product.php?id=$1
RewriteRule ^page/([0-9a-zA-Z]+) page.php?page_id=$1

This allows me to access the page category.php?id=1 at category/1 the same for the other pages I have rewrite rules for. However my php script can no longer access the $_GET variable from the URL so it is rewriting correctly but now unable to show any of the content?

3
  • Did you debug $_GET (var_dump) already? Commented Mar 21, 2018 at 15:29
  • try RewriteRule ^(category|product|page)/([0-9a-zA-Z]+) $1.php?id=$2 [PT] Commented Mar 21, 2018 at 15:31
  • clear browser cache then test again Commented Mar 21, 2018 at 15:40

1 Answer 1

4

You will need to turn MultiViews option off and also do little bit refactoring of your rules.

Options -MultiViews
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

RewriteRule ^category/([0-9a-zA-Z]+)/?$ category.php?id=$1 [L,NC,QSA]
RewriteRule ^product/([0-9a-zA-Z]+)/?$ product.php?id=$1 [L,NC,QSA]
RewriteRule ^page/([0-9a-zA-Z]+)/?$ page.php?page_id=$1 [L,NC,QSA]

Option MultiViews (see http://httpd.apache.org/docs/2.4/content-negotiation.html) is used by Apache's content negotiation module that runs before mod_rewrite and makes Apache server match extensions of files. So if /file is the URL then Apache will serve /file.html.

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

2 Comments

GitHub Copilot, ChatGPT, Gemini, and regular Bing CoPilot all failed to suggest Options -MultiViews, which fixed it for me. I also mentioned I was using WAMP during the debugging. Looks like we still need good ol' stack overflow.
AI vs HI (human intelligence). No prize for guessing who will come out on top :-)

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.