Search code examples
.htaccessurl-rewritingslash

force htaccess to rewrite url from last slash


I need to use htaccess to change below url:

http://example.com/main/en/index.php?page=pages&page_id=9

to:

http://example.com/main/en/pages/9.html

I've done it by the below rule:

RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)\.html$ /main/fa/?index.php?page=$1&page_id=$2 [L]

but there is a problem, this rule make my display url to:

http://example.com/pages/9.html

I want to htaccess change url just after last slash.

Because I must have more than one language, it will have conflict with other languages. How must I write this rule?


Solution

  • Maybe this is what you are looking for:

    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI}  ([^/]+)/([^/]+)/([\d]+)\.html/?   [NC]
    RewriteRule  .*          main/%1/index.php?page=%2&page_id=%3 [L]
    

    Will redirect internally:

    http://example.com/main/LangCode/PageName/PageID.html

    To:

    http://example.com/main/LangCode/index.php?page=PageName&page_id=PageID