Mod Rewrite query string to clean url

Hey guys,

Quick question, I have a page which I am trying to mod rewrite to produce a clean url, however I cannot seem to get it to work in htaccess. The old url is:

http://<ip>/test/view?q=<id>

And I would like it to go to:

http://<ip>/test/view/<id>

The current rule is as follows:

RewriteEngine On
# remove php extension if not a physical file or directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) $1.php [L]

#rewrite URL
RewriteRule ^test/view/$ test/view?q=$1 [L]

Open in new window


This is on a private server so I don't need to worry about duplicate content etc.

Any pointers or help would be most awesome :)

Thanks,

Michael.
LVL 5
MichaelT_Asked:
Who is Participating?
 
arober11Commented:
Hi

For a start your new rule needs to appear before your catch all:

...
RewriteRule (.*) $1.php [L]

Else it will never be reached. Also your lacking the expression to instantiate $1, so your q= CGI variable would always be blank, if the rule were reachable. Anyway a very quick hack and the following will tick a few more boxes.

RewriteEngine On

#rewrite URL
RewriteRule ^test/view/(.*)$   test/view?q=$1 [L]

# remove php extension if not a physical file or directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*)  $1.php [L]

Open in new window

0
 
Julian HansenCommented:
How about this?

...
RewriteRule ^test/view?q=(.*) test/view/$ [L]

Open in new window

0
 
MichaelT_Author Commented:
Hi JulianH,

Thanks for taking for taking the time to look at the question and for your response :)

Unfortunately I still seem to get a 500 Internal Server Error when I add it to .htaccess and then try and access:

<a href=view/<id> >View Record</a>  For example <a href=view/555 >View Record</a>

I removed "test" from the path (it is the current directory where the two php files sit) and now my htaccess looks like:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) $1.php [L]

RewriteRule ^view?q=(.*) view/$ [L]

Open in new window


I have attached a couple of test files I was using to test out the rule as well as the current .htaccess file, it may make it easier to debug what is going wrong.  

Again thanks for your help :)
test.zip
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.