The .htaccess redirection rule leads me to the wrong place

I'm having trouble redirecting URLs using a .htaccess file. This is what my .htaccess file looks like:

Redirect 301 /file-name/example.php http://www.mysite.com/file-name/example-001.php
Redirect 301 /section-name/example.php http://www.my-site.com/section-name/example-002.php

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.mysite.com$ [NC]
RewriteRule ^(.*)$ http://www.mysite.com/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)/(.*)$ hqtemplates/articles.php?file_name=$2 [L]
php_value session.use_only_cookies 1
php_value session.use_trans_sid 0

Now the problem is that when I go to the page:, www.my-site.com/file-name/example.phpinstead of redirecting me to, www.my-site.com/file-name/example-001.phpit redirects me to www.my-site.com/file-name/example.php?file_name=example-001.php.

For some reason, it adds "? File_name = example-001.php" to the url . Does anyone know why this is happening and how to fix it?

+3
source share
1 answer

mod_alias mod_rewrite. . mod_rewrite ( ):

RewriteEngine on
# emulate specific mod_alias Redirect rules
#
# Flags explanation:
#   [L] = last rule, stop processing further rules
#   [R=301] = 301 Redirect
#
RewriteRule ^file-name/example.php$ http://www.mysite.com/file-name/example-001.php [L,R=301]
RewriteRule ^section-name/example.php$ http://www.my-site.com/section-name/example-002.php [L,R=301]

# handle other rewrite requests
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)/(.*)$ hqtemplates/articles.php?file_name=$2 [L]

.htaccess, :

(2) rewrite 'file-name/example.php' -> \
        'hqtemplates/articles.php?file_name=example.php'
(2) strip document_root prefix: /home/test/hqtemplates/articles.php -> \
         /hqtemplates/articles.php
(1) internal redirect with /hqtemplates/articles.php [INTERNAL REDIRECT]
(1) pass through /home/test/Sites/file-name/example-001.php

:

  • mod_rewrite "RewriteRule ^ (. +)/(. *) $hqtemplates..."
  • file-name/example.php hqtemplates/articles.php?file_name=example.php
  • mod_alias " 301..."
  • file-name/example-001.php?file_name=example.php
+3

Source: https://habr.com/ru/post/1699431/


All Articles