开发者

How to make a better URL with .htaccess and multiple parameters?

I have a very long a unfriendly URL an I'm looking to make SEO better for the site:

http://www.site.com/sub-site/index.php?page=nameofpage&locale=en_EN

I would like to have this instead: http:// www.site.com/sub-site/en/nameofpage

开发者_如何学运维all the URLS are hard coded in the links in the form of:

<a href="index.php?page=nameofpage&locale=en_EN">link</a>

What is the best way to achieve this with mod-rewrite? I find it particularly difficult to achieve:

  • combining both parameters, page and locale
  • Turning ISO locales EN_en to en in the URL
  • How should I write links? using the result URL or the index.php?page... controller scheme?

EDIT: I'm going mad! I can't make it work!! Feel so stupid... but, Could anyone help me with the exact .htaccess file I need to pull this off? Thanks!


mod_rewrite is an incredibly complex beast. This should get you started, though! Just read up on the documentation and play around with it. You'll get the hang of it soon enough!

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    # Checks to see if the user is attempting to access a valid file,
    # such as an image or css document. If this isn't true, it sends the
    # request to index.php
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/(.*)$ index.php?locale=$1&page=$2
    RewriteRule ^(.*)$      index.php?page=$1
</IfModule>

Edit: You should copy and paste this into a .htaccess file.


EDIT: I went through and tested this out on one of my servers. This works fine for me:

RewriteEngine On
RewriteRule ^sub-site/([a-z_]+)/([a-z0-9_-]+)$ /sub-site/index.php?locale=$1&page=$2

This gets you most of the way there, but it doesn't do the locale translation (en => en_EN), it just delivers the locale as-is to the PHP script. Using mod_rewrite, I'd recommend RewriteMap; however, if you only have access to .htaccess (like on a shared host), you can't use RewriteMap.

So then, to accomplish the locale translation, I think you'd be best off doing it on the PHP side. You could either use one of the frameworks listed below, or just do it yourself.

In case you do have access to the virtual host configuration, you'd make a text file like:

en en_EN
fr fr_FR
ca fr_CA

If that file was located at /path/to/localemap.txt, you'd have:

RewriteMap locales txt:/path/to/localemap.txt
RewriteRule ^sub-site/([a-z_]+)/([a-z0-9_-]+)$ /sub-site/index.php?locale=${locales:$1|en_EN}&page=$2

This defaults to en_EN if nothing's found in the map.

Hope that helps!


If you're just using .htaccess, then mod_rewrite will easily do what you want.

  • http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html
  • http://articles.sitepoint.com/article/guide-url-rewriting
  • http://www.addedbytes.com/cheat-sheets/mod_rewrite-cheat-sheet/

You'd probably want to look into RewriteMap for your categories or locale, although you could just grab that part of the URL and use it as a parameter directly. Here's an example to get you started:

RewriteRule /sub-site/([a-z_]+)/([a-z0-9_]+) /sub-site/index.php?locale=$1&page=$2

You'd really want to narrow down the regular expressions used to make them more specific to your supported locales and categories, but I think it's a decent starting point.

Other PHP frameworks, especially those focused on MVC, also provide similar URL routing functionality. Examples:

  • CakePHP: http://book.cakephp.org/view/46/Routes-Configuration
  • CodeIgniter: http://codeigniter.com/user_guide/general/routing.html
  • Kohana: http://docs.kohanaphp.com/general/routing
  • Yii: http://www.yiiframework.com/doc/guide/topics.url
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜