Search code examples
regexapache.htaccessmod-rewritemodx-revolution

.htaccesss RewriteRule won't work


I want rewrite

htttp://www.site.ru/company.html?name=bestcompany

to

htttp://www.site.ru/company/bestcompany.html

Please suggest where I was wrong. What I do:

RewriteRule ^company/([^/]*)\.html$ /company.html?name=$1 [L]

CMS modx, .htacces in root folder, full .htaccess code:

RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\.site\.ru [NC]
RewriteRule (.*) http://www.site.ru/$1 [R=301,L]

# The Friendly URLs part
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

RewriteRule ^company/([^/]*)\.html$  /company?name=$1 [L]

Solution

  • Rearrange your rules like this:

    RewriteEngine On
    RewriteBase /
    
    RewriteCond %{HTTP_HOST} .
    RewriteCond %{HTTP_HOST} !^www\.site\.ru [NC]
    RewriteRule (.*) http://www.site.ru/$1 [R=301,L,NE]
    
    RewriteCond %{THE_REQUEST} \s/+(company)(?:\.html)?\?name=([^\s&]+) [NC]
    RewriteRule ^ /%1/%2.html? [R=302,L,NE]
    
    RewriteRule ^company/([^./]+)\.html$  /company?name=$1 [L,NC,QSA]
    
    # The Friendly URLs part
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]