htaccess – Custom CNC does not work in MODx

Question:

Good afternoon. It is necessary that links of the form

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

replaced by a link

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

Please tell me where I went wrong. What have you been doing:

Wrote accordingly:

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

Worth Modx. .htaccess at root, friendly urls work, 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.html?name=$1 [L]

Answer:

You are wrong in the order of the rules.

Here's the part:

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

Fires before your rule:

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

If you enter htttp://www.site.ru/company/bestcompany.html , then after the standard rules, the original URL looks like this: http://www.site.ru/index.php?q=company/bestcompany.html and not satisfies the conditions of your redirect.

The solution to the problem is very simple: swap redirects. Like this:

RewriteEngine On
RewriteBase /

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

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

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

After that, everything should work.

Scroll to Top