?
Solved

Mod Rewrite query string to clean url

Posted on 2012-08-14
3
Medium Priority
?
734 Views
Last Modified: 2012-09-18
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.
0
Comment
Question by:MichaelT_
3 Comments
 
LVL 60

Expert Comment

by:Julian Hansen
ID: 38295058
How about this?

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

Open in new window

0
 
LVL 5

Author Comment

by:MichaelT_
ID: 38299045
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
 
LVL 26

Accepted Solution

by:
arober11 earned 1500 total points
ID: 38384643
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

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
What You Need to Know when Searching for a Webhost Provider
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses
Course of the Month17 days, 13 hours left to enroll

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question