重写url的htaccess mod_rewrite问题

时间:2021-01-23 15:32:07

标签: apache .htaccess mod-rewrite url-rewriting friendly-url

我正在尝试将 url 地址重定向到一个不错的 url。不幸的是,经过几次尝试后,它仍然无法与此 htaccess 一起使用,您能以某种方式给我建议吗?

我的 htaccess

Options +FollowSymlinks
RewriteEngine On


RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]


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


RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ /$1.html



RewriteCond %{REQUEST_URI} !^subdom/
RewriteCond %{REQUEST_URI} !^/subdom/
RewriteCond %{HTTP_HOST} ^(www\.)?(.*)\.([^\.]*)\.([^\.]*)$
RewriteCond %{DOCUMENT_ROOT}/subdom/%2 -d
RewriteRule (.*) subdom/%2/$1 [DPI]


RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^domains/[^/]+/(.+[^/])$ /$1/ [R]

RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^subdom/[^/]+/(.+[^/])$ /$1/ [R]

这不起作用

RewriteCond %{QUERY_STRING} ^page=([^&]+)$
RewriteRule ^index\.php$ %1? [R=301,L,NE]
RewriteRule ^([^/]+)\.html index.php?page=$1 [L,QSA]

这不起作用

RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f


RewriteRule ^([^/]+)/([^/]+)/(.+)$ index.php?lang=$1&page=$2 [L,QSA]
RewriteRule ^([^/]+)/(.+)$ index.php?lang=$1 [L,QSA

我需要用这个制作 https://mypage.com/index.php?page=my-pagehttps://mypage.com/index.php?lang=en&page=my-page

这个 https://mypage.com/my-page/https://mypage.com/en/my-page/

1 个答案:

答案 0 :(得分:2)

使用您显示的示例,请尝试关注 htaccess。在这里添加了 OP 已经存在的规则(我删除了 OP 的规则 RewriteCond %{REQUEST_URI} !^subdom/,因为 REQUEST_URI 变量总是从 / 开始,因此需要此规则)并附加在其中的建议规则。请确保在测试您的网址之前清除浏览器缓存。

Options +FollowSymlinks
RewriteEngine ON

RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

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

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ /$1.html

RewriteCond %{REQUEST_URI} !^/subdom/
RewriteCond %{HTTP_HOST} ^(www\.)?(.*)\.([^\.]*)\.([^\.]*)$
RewriteCond %{DOCUMENT_ROOT}/subdom/%2 -d
RewriteRule (.*) subdom/%2/$1 [DPI]
   
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^domains/[^/]+/(.+[^/])$ /$1/ [R]

RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^subdom/[^/]+/(.+[^/])$ /$1/ [R]

####Newly added rules here....

RewriteCond %{QUERY_STRING} ^page=(.*)/?$ [NC]
RewriteRule ^index\.php/?$ %1 [R=301,NC]
RewriteRule ^(.*)/?$ index.php?page=$1 [L]

RewriteCond %{QUERY_STRING} ^lang=([^&]*)&page=(.*)/?$ [NC]
RewriteRule ^index\.php/?$ %1/%2 [R=301,NC]
RewriteRule ^([^/]*)/(.*)/?$ index.php?lang=$1&page=$2 [L]