Using .htaccess
from my previous question, I edited it to add a rewrite rule for rewriting /contact/
to /contact.php
and /xyz/contact/
to /contact.php?lang=xyz
. While the second one works, the first still looks for the actual directory which doesn't exist, returning 404
code. Both redirects to the nice URL variant work just like expected. Here is my .htaccess
settings for that:
# No directory listing, no multi views, follow symlinks
Options -Indexes -MultiViews +FollowSymLinks
# Redirects and rewrites allowed
RewriteEngine on
# ...
# Redirect direct requests for "contact.php?lang=xyz" to "/xyz/contact/"
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^lang=([a-z]{2,3})$
RewriteRule ^contact\.php$ /%1/contact/? [R=301,L]
# Redirect direct request for "contact.php" to "/contact/"
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^contact\.php$ /contact/? [R=301,L]
# Internally rewrite "/xyz/contact/" to "/contact.php?lang=xyz"
RewriteRule ^([a-z]{2,3})/contact/?$ /contact.php?lang=$1 [L]
# ...
# Internally rewrite "/contact/" to "/contact.php"
RewriteRule ^/contact/?$ /contact.php [L]
# Переписать внутренне "/ contact /" на "/contact.php" RewriteRule ^ / contact /? $ /Contact.php [L]
В файлах для каждого каталога .htaccess
вам необходимо удалить префикс косой черты в шаблоне RewriteRule
(как вы это делали в предыдущих директивах). Таким образом, это должно быть записано как:
RewriteRule ^contact/?$ /contact.php [L]
Это соответствует запросам для / contact
и / contact /
.
Префикс косой черты не используется в RewriteRule
шаблон , потому что в контексте .htaccess
префикс каталога (который, в частности, заканчивается косой чертой) сначала удаляется из URL-пути, который RewriteRule
шаблон совпадает с. ( префикс каталога - это путь в файловой системе, в котором находится файл .htaccess
.)