How can I display friendly urls using mod_rewrite?

I am not a PHP developer at heart, and I have been asked to do some SEO on an existing PHP website.

The first thing I noticed was the ugly URLs, so I want them to correspond in something more informative. Here are all the possible patterns:

/index.php?m=ModuleType&categoryID=id /index.php?m=ModuleType&categoryID=id&productID=id /index.php?page=PageType /index.php?page=PageType&detail=yes 

So basically, what I want to do is convert them into something like:

 /ModuleType/Category /ModuleType/Category/ProductName /Page /Page 

I did not use mod_rewrite before any tips or examples would be great!

Thanks.

+4
source share
3 answers

mod_rewrite will most likely be used for the opposite: rewrite requests /ModuleType/Category/ProductName internally with /index.php?m=ModuleType&categoryID=id&productID=id . Using new URLs in documents is your application’s job.


Change Here's an example of what a function looks like that turns your parameterized URLs into new ones:

 function url($url, $rules) { $url = parse_url($url); parse_str($url['query'], $url['query']); $argNames = array_keys($url['query']); foreach ($rules as $rule) { if ($rule[0] == $url['path'] && array_keys($rule[1]) == $argNames) { $newUrl = $rule[2]; foreach ($rule[1] as $name => $pattern) { if (!preg_match('/'.addcslashes($pattern, '/').'/', $url['query'][$name], $match)) { continue 2; } $newUrl = str_replace('<'.$name.'>', $match[0], $newUrl); } return $newUrl; } } return $url; } $rules = array( array( '/index.php', array('m'=>'.*', 'categoryID'=>'.*', 'productID'=>'.*'), '/<m>/<categoryID>/<productID>' ) ); echo '<a href="' . url('/index.php?m=ModuleType&categoryID=categoryID&productID=productID', $rules) . '">/ModuleType/Category/ProductName</a>'; 
+1
source

I'm not an expert on mod_rewrite, but this is an example of how I put together .htaccess for the Image Flair site:

 <IfModule mod_rewrite.c> RewriteEngine On RewriteRule ^(.*)/(.*)\.png$ imageFlair.php?mode=$1&userid=$2 [L] RewriteRule ^(.*)\.png$ imageFlair.php?userid=$1 [L] </IfModule> 

These are mainly cards:

MODE / USERID.png →? ImageFlair.php mode = MODE & = USERID

and

USERID.png → imageFlair.php? UserID = USERID

You should be able to adapt this to your needs, but you may have several problems:

  • If you want to use "names" rather than identifiers in your URL, you will need to modify PHP to accept the names.
  • You may have a problem with the / Page and / ModuleType conflicts if you also want to include more parameters in the parameter using the page, if you cannot collect a regular expression that can determine what is.

Going to the list of URLs you want, this should work, although I will not argue that this is the best or only way to do this :-)

 <IfModule mod_rewrite.c> RewriteEngine On RewriteRule ^(.*)/(.*)/(.*)$ index.php?m=$1&categoryID=$2&productID=$3 [L] RewriteRule ^(.*)/(.*)$ index.php?m=$1&categoryID=$2 [L] RewriteRule ^(.*)$ index.php?Page=$1 [L] </IfModule> 

As suggested, you may want to replace. * to [^ /] +, but I had problems with inconsistencies when I did this, and there was no time to troubleshoot, so YMMV :-)

+1
source

It’s not entirely clear from your message what variables are. But assuming ModuleType , id (x2), and Page are all variables, then the following rules with backlinks should work in the .htaccess file.

 RewriteEngine On RewriteRule ^([^/]+)/([^/]+)$ /index.php?m=$1&categoryID=$2 [L] RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ /index.php?m=$1&categoryID=$2&productID=$3 [L] RewriteRule ^([^/]+)$ /index.php?page=PageType [L] RewriteRule ^([^/]+)/detail$ /index.php?page=PageType&detail=yes [L] 

The latter did not make any sense since you wrote it. So instead you can add /detail to the end.

They should slip right on top of existing applications without any changes to the application. Since they are not redirected using [R] , it will be transparent to your users.

+1
source

All Articles