?
Solved

.htaccess mod_rewrite and PHP

Posted on 2009-04-20
11
Medium Priority
?
396 Views
Last Modified: 2012-05-06
Hello all, we have a PHP application and would like to add friendly URL's to it.

We're trying to use mod_rewrite to accomplish it but not having luck with it.

The system always calls the same file index.php with variables to include the correct information, so some typical URLs would be:
index.php?page=test&task=view
index.php?page=test&task=edit&id=1
index.php?page=xpto&task=view
index.php?page=xpto&task=edit&id=1
and so on...

When using: RewriteRule ^(.*)/(.*)/(.*?).html$ index.php?page=$1&task=$2&id=$3
the URL: index.php?page=test&task=edit&id=1 works but index.php?page=test&task=view doesn't work.

Thanks in advance.
0
Comment
Question by:componentslab
  • 6
  • 5
11 Comments
 
LVL 51

Expert Comment

by:ahoffmann
ID: 24191917
please post an example of an URL comming from the browser and the corresponding example it should be rewritten too
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 24192475
RewriteRule ^(.*)/(.*)/([0-9]+)\.html$ index.php?page=$1&task=$2&id=$3 [L]
RewriteRule ^(.*)/(.*)\.html$ index.php?page=$1&task=$2
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:componentslab
ID: 24194535
Will try it out, is there a way to make extra variables to appear in the front of the URL automatically? For example:

http://localhost/test/view.html?orderby=1&order=ASC
http://localhost/index.php?page=test&task=view&orderby=1&order=ASC
0
 
LVL 51

Accepted Solution

by:
ahoffmann earned 1000 total points
ID: 24194710
RewriteRule ^(.*)/(.*)/([0-9]+)\.html$ index.php?page=$1&task=$2&id=$3&order=ASC [L]
RewriteRule ^(.*)/(.*)\.html$ index.php?page=$1&task=$2&order=ASC
0
 

Author Comment

by:componentslab
ID: 24194746
The order can by ASC or DESC as well as it can exist other variables so if we could place the ones we know that always exist in the rules and any extra variables to appear in the end it would make it easy.
0
 

Author Comment

by:componentslab
ID: 24195014
I tested and seems to work, with the exception of the extra variables that exist only some times. Another thing I've noticed is that some images disappear with this.
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 24195193
> Another thing I've noticed is that some images disappear with this.
then you need a RewriteCond to exclude the image URLs

> .. with the exception of the extra variables that exist only some times
please post an example
0
 

Author Comment

by:componentslab
ID: 24195198
This worked out for the other variables apparently:

RewriteRule ^(.*)/(.*)/([0-9]+)\.html$ index.php?page=$1&task=$2&id=$3&%{QUERY_STRING} [L]
RewriteRule ^(.*)/(.*)\.html$ index.php?page=$1&task=$2&%{QUERY_STRING}

Will need to make more tests. Regarding the images is there anything we can do in the .htaccess? The images that have as source the complete URL appear the others don't.
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 24195315
%{QUERY_STRING} is a variable from the RewriteCond
IIRC it will not work this way, use the QSA flag instead
0
 

Author Comment

by:componentslab
ID: 24196381
Like this:

RewriteRule ^(.*)/(.*)/([0-9]+)\.html$ index.php?page=$1&task=$2&id=$3 [L,QSA]
RewriteRule ^(.*)/(.*)\.html$ index.php?page=$1&task=$2 [L,QSA]

Apparently works :)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

If you are a web developer, you would be aware of the <iframe> tag in HTML. The <iframe> stands for inline frame and is used to embed another document within the current HTML document. The embedded document could be even another website.
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month15 days, 8 hours left to enroll

850 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