Whats the ideal solution for writing the urls in this case with mod rewrite?

[php]
if($temp == 1){

}else if($temp == 2){
   echo section($id);
}else if($temp == 3){
   if(count($pid) == 3){
       $art = $pid[2];
       echo article_section($id, $temp, $art);
     }else{
       echo  article_list($id, $temp, 100);
     }

}else if($temp == 4){
contact();
}else{
echo “ERROR 404”;
}
[/php]

This is my php code and the url looks something like this [php]home.php?pid=1,2,3[/php] but I want to make it all text no numbers and be more like
[php]home.php/home/example/article-here[/php]

So how can I replace the numbers where I ask is it equal to 1,2 or 3 with text ?

[php]

RewriteEngine On

RewriteBase home.php

RewriteRule ^([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)$ home.php?pid=$1/$2/$3

</If Module>
[/php]

Well, I have been researching this a bit over the last week to find a solution for you. I was thinking of one of my
sites where I really need to do this and had a large number of sites bookmarked to review to decide how best to
handle it myself. I was just about ready to give up and just do it manually like you have. Then, I found the site
below. It is not really an answer for you, but, it might be useful. It seems like it is how most are doing it now!

It explains a great way to handle the redirecting using only a small line in the re-write code that sends users to
one file that determines the redirection using the database. This is great, because you can have the file track all
url’s that are typed into your site and you can set up a database admin page to control the url changes online.
http://code.tutsplus.com/tutorials/using-htaccess-files-for-pretty-urls–net-6049

Now, also, back to your version, here are several sites that discuss this process and seem to be good ones to
read. Personally, I am going to test the above one and see if it will work for me. I also found a tricky way you
can do redirection using a 404 page that goes to a PHP file and then uses that to redirect without use of the
re-write system. But, it does not really display the URL the way I would like. Anyway, here is the list of a few
to read. Not sure if all this helps, but, maybe…
http://pixelcode.co.uk/tutorials/webmaster/clean-urls-with-mod_rewrite/
( This one makes a comment about the [L] flag that I forget about… It helps if you order rules correctly!)

http://forum.codecall.net/topic/74170-clean-urls-with-php/
( This one uses the same technique as the first post above, but, goes into a lot of detail to explain it all…)

http://www.the-art-of-web.com/system/rewrite/1/
( More in the line of your way, but, has some nicely highlighted explanations of how to do it. )

After reading all of these and a hundred other sites, I think the best is the first way or the next to last link. It does
seem that this is the way everyone is doing it now, just from what I have read. Perhaps one of the programmers
here with more info on this will jump in to help further. I am planning to put this version on one of my servers
for further testing. But, it seems this is better than complicated rewrites done in the .htaccess file… Well, not
sure if all this helps, but, lots of info in all this…

This is a question which can be solved in most frameworks, but if you are just going to implement it simply here is a good start.

RewriteEngine On

# Necessary to prevent problems when using a controller named "index" and having a root index.php
# more here: http://httpd.apache.org/docs/2.2/content-negotiation.html
Options -MultiViews

# Activates URL rewriting (like myproject.com/controller/action/1/2/3)

# Disallows others to look directly into /public/ folder
Options -Indexes

# When using the script within a sub-folder, put this path here, like /mysubfolder/
# If your app is in the root of your web folder, then leave it commented out
RewriteBase /

# General rewrite rules
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ home.php?url=$1 [QSA,L]

What this code does it turns the string after the domain into a GET variable accessible with $_GET[‘url’]
If for example your URL looks like this http://mywebsite.com/1/2/3

$_GET[‘url’] will return 1/2/3.

You can break this apart by going
[php]
if (isset($_GET[‘url’])) {
$url_parts = explode(’/’, $_GET[‘url’]);
}
[/php]

The above code will give you an array of url parts with $url_parts[0] being the first variable. Most people who go down this path use some type of Model View Controller (MVC) to instantiate a Controller Class with the first variable then call a method with the second var and add any arguments to the method with the remainder.

If you would like some help going down that path, let me know.

Hope this helps

Sponsor our Newsletter | Privacy Policy | Terms of Service