1

I am just trying to get mod_rewrite working and I am having a massive mental block where nothing seems to be working.

My .htaccess file hides the file extension of my files and I'm not sure if this is why my rewrite isn't working.

Here is my .htaccess code:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1.php?project=$2
Options +FollowSymLinks

With the rewrite rule I am trying to get projects.php?project=1 to display as projects/project/1

Would really appreciate it if someone could enlighten me as to what I am doing wrong.

2 Answers 2

1

Problem is not ending the rules with L (Last) flag and ordering of your rules. Replace your code with:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteRule ^([^/]+)/([^/]+)/$ /$1.php?project=$2 [L,QSA]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
Sign up to request clarification or add additional context in comments.

4 Comments

This doesn't seem to be working either. The url still displays a query string.
projects/project/1 am I missing something?. New to doing this.
You wrote: The url still displays a query string but in your URI: projects/project/1 there is no query string. So I'm a bit confused on nature of your problem.
I am trying to get the query string in my URL to change using mod_rewrite. Currently projects.php takes in a query string of projects=number. So I want projects.php?project=number to display to the user as projects/project/1.
0

The RewriteConds are applied to the first rule only. If you want the same conditions for the second rule you must duplicate them.

Order is important. The first rule is applied first and rewrites /projects/project/1 to /projects/project/1.php and then again to /projects/project/1.php.php and /projects/project/1.php.php.php, ..., because the condition /path/to/projects.php -f is always true. You must add an additional condition to prevent this endless loop.

The second rule is never tried because of this.

Finally, the regular expression ^([^/]+)/([^/]+)/$ doesn't match projects/project/1. You must add a trailing (\d+) to capture the project number as $3

RewriteRule ^([^/]+)/([^/]+)/(\d+)$ /$1.php?project=$3

or

RewriteRule ^([^/]+)/([^/]+)/(\d+)$ /$1.php?$2=$3

if the rule should be more general.

4 Comments

Thanks for the reply. It doesn't seem to be working for me. What bits should I take out and replace? I'm very new to doing this as I have never really used it before.
@MarkBlythe Remove your second rule and insert one of the rules in my answer before your first rule.
@MarkBlythe Do you get an error message? Or do you want to change the address in the browser's bar?
I was getting error 500 however not it's working but not changing the url to look nice. Any pointers?

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.