嗨,我有這樣的鏈接:
www.example.com/a-letter/a-1.html
www.example.com/b-letter/b-1.html
a letter 和 b-letter 檔案夾下也有 3 個檔案。我a-letter用這個 .htaccess 代碼洗掉了部分:
##
RewriteCond %{THE_REQUEST} /a-letter/ [NC]
RewriteRule ^a-letter/(.*)$ /$1 [L,R=301,NC,NE]
##
b-letter并使用此 .htaccess 代碼洗掉部分:
##
RewriteCond %{THE_REQUEST} /b-letter/ [NC]
RewriteRule ^b-letter/(.*)$ /$1 [L,R=301,NC,NE]
##
我正在a-1 a-2 a-3使用此 htaccess 代碼重定向所有檔案:
RewriteEngine on
RewriteRule ^/?a-letter/(.*)(/|\.html)?$ /$1 [R=301,L]
RequestCond %{DOCUMENT_ROOT}/a-letter%{REQUEST_URI}.html -f
RewriteRule ^/?(a-1|a-2|a-3)/? /a-letter/$1.html [END]
我也b-1 b-2 b-3用這個 htaccess 代碼重定向所有檔案:
##
RewriteEngine on
RewriteRule ^/?b-letter/(.*)(/|\.html)?$ /$1 [R=301,L]
RequestCond %{DOCUMENT_ROOT}/b-letter%{REQUEST_URI}.html -f
RewriteRule ^/?(b-1|b-2|b-3)/? /b-letter/$1.html [END]
##
All these codes in the same .htaccess file and when i want to open www.example.com/a-1.html page opens correctly . My problem is although I use the same codes for b-letter, but I cannot open the link www.example.com/b-1 . I got 404 not found error. When i replace the codes for b-letter to the top of the .htaccess file this time www.example.com/b-1 opens correctly but the www.example.com/a-1 link does not open which ones codes at below in .htaccess file. I tried to change [END] flag to [L] it did not work for me. What can i do in this situation? Is there any way for the disable importance of sorting in .htaccess file . I did a lot of research on stackoverflow and other sites, but I couldn't find any results working for me. I will be grateful if you could help me. Thanks from now.
UPDATE:
I also share my full code of .htaccess file below by request of @anubhava
#Redirection http to https start#
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
#Redirection http to https end#
##301 Redirection Start##
Redirect 301 /index.html https://www.example.com/
Redirect 301 /index https://www.example.com/
##301 Redirection End##
##adding www to links start##
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301,NC]
##adding www to links end##
#example.com/page will display the contents of example.com/page.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(. )$ $1.html [L,QSA]
##
#301 from example.com/page.html to example.com/page
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.html\ HTTP/
RewriteRule ^(.*)\.html$ /$1 [R=301,L]
##
------------------------------------------------------------------------
# remove a-letter from URLs start#
RewriteCond %{THE_REQUEST} /a-letter/ [NC]
RewriteRule ^a-letter/(.*)$ /$1 [L,R=301,NC,NE]
# remove a-letter from URLs end#
## Rewrite non a-letter links start##
RewriteEngine on
RewriteRule ^/?a-letter/(.*)(/|\.html)?$ /$1 [R=301,L]
RequestCond %{DOCUMENT_ROOT}/a-letter%{REQUEST_URI}.html -f
RewriteRule ^/?(a-1|a-2|a-3|a-4|a-5|a-6|a-7|a-8)/? /a-letter/$1.html [END]
## Rewrite non a-letter links start##
# remove comments/a/ from links start#
RewriteCond %{THE_REQUEST} comments/a/ [NC]
RewriteRule ^comments/a/(.*)$ /$1 [L,R=301,NC,NE]
# remove comments/a/ from links end#
## Rewrite non comments/a/ links start##
RewriteEngine on
RewriteRule ^/?comments/a/(.*)(/|\.html)?$ /$1 [R=301,L]
RequestCond %{DOCUMENT_ROOT}/comments/a%{REQUEST_URI}.html -f
RewriteRule ^/?([^.?] )$/? /comments/a/$1.html [END]
##Rewrite non comments/a/ links end##
------------------------------------------------------------------------
# remove b-letter from URLs start#
RewriteCond %{THE_REQUEST} /b-letter/ [NC]
RewriteRule ^b-letter/(.*)$ /$1 [L,R=301,NC,NE]
# remove b-letter from URLs end#
## Rewrite non b-letter links start##
RewriteEngine on
RewriteRule ^/?b-letter/(.*)(/|\.html)?$ /$1 [R=301,L]
RequestCond %{DOCUMENT_ROOT}/b-letter%{REQUEST_URI}.html -f
RewriteRule ^/?(b-1|b-2|b-3|b-4|b-5|b-6|b-7|b-8)/? /b-letter/$1.html [END]
##Rewrite non b-letter links end##
#remove comments/b/ from links start#
RewriteCond %{THE_REQUEST} comments/b/ [NC]
RewriteRule ^comments/b/(.*)$ /$1 [L,R=301,NC,NE]
# remove comments/b/ from links end#
## Rewrite non comments/b/ links start##
RewriteEngine on
RewriteRule ^/?comments/b/(.*)(/|\.html)?$ /$1 [R=301,L]
RequestCond %{DOCUMENT_ROOT}/comments/b%{REQUEST_URI}.html -f
RewriteRule ^/?([^.?] )$/? /comments/b/$1.html [END]
## Rewrite non-comments/b/ links end##
------------------------------------------------------------------------
uj5u.com熱心網友回復:
嗨,我這樣解決了我的問題:
首先我的網站鏈接是這樣的:
www.example.com/a-letter/a-1.html
www.example.com/b-letter/b-1.html
www.example.com/comments/a/word-abc.html
www.example.com/comments/b/word-xyz.html
我a-letter使用以下代碼從鏈接目錄中洗掉:
# remove /a-letter/ from URLs
RewriteCond %{THE_REQUEST} /a-letter/ [NC]
RewriteRule ^a-letter/(.*)$ /$1 [L,R=301,NC,NE]
而b-letterforcomments/a/和comments/b/like上面只有字母不同。
我在內部更改了重寫規則,/a-1 /a-2 /a-3如下所示:(此外,我的托管服務使用 lite speed 服務器,因為這個原因我寫了<IfModule LiteSpeed>intead <ifModule mod_rewrite.c>)這個代碼www.example.com/a-1顯示www.example.com/a-1。 html內部的內容。鏈接沒有變化。
<IfModule LiteSpeed>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(a-1|a-2|a-3|a-4|a-5|a-6|a-7|a-8)/? a-letter/$1.html [L]
</IfModule>
我也改變了 b-letter和b-1 b-2 b-3上面一樣,只有字母不同。
/comments/a/我用這段代碼在內部重寫了非鏈接:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/comments/a/$1.html -f
RewriteRule ^/?([^.?] )$/?$ comments/a/$1.html [L,NC]
對于像上面這樣的非comments/b/鏈接,只有字母不同。
我.htaccess的檔案內容按順序如下:
##Redirect http to https start##
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
##Redirect http to https end##
##301 Redirect Start##
Redirect 301 /index.html https://www.example.com/
Redirect 301 /index https://www.example.com/
##301 Redirect End##
##Remove .html extension start##
##example.com/page will display the contents of example.com/page.html##
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(. )$ $1.html [L,QSA]
##
##301 from example.com/page.html to example.com/page##
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.html\ HTTP/
RewriteRule ^(.*)\.html$ /$1 [R=301,L]
##
##Remove .html extension end##
------------------------------------------------------------------------
##remove /a-letter/ from link ##
RewriteCond %{THE_REQUEST} /a-letter/ [NC]
RewriteRule ^a-letter/(.*)$ /$1 [L,R=301,NC,NE]
##
.
.
.
##Rewrite non /a-letter/ links internally start##
<IfModule LiteSpeed>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(a-1|a-2|a-3|a-4|a-5|a-6|a-7|a-8)/? a-letter/$1.html [L]
</IfModule>
##Rewrite non /a-letter/ links internally end##
.
.
.
##remove /comments/a/ from link start##
RewriteCond %{THE_REQUEST} comments/a/ [NC]
RewriteRule ^comments/a/(.*)$ /$1 [L,R=301,NC,NE]
##remove /comments/a/ from link end##
.
.
.
##Rewrite non /comments/a/ links internally start##
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/comments/a/$1.html -f
RewriteRule ^/?([^.?] )$/?$ comments/a/$1.html [L,NC]
##Rewrite non /comments/a/ links internally end##
.
.
.
------------------------------------------------------------------------
這段代碼一切正常,我的網站有完整的友好網址。特別感謝@arkascha和@anubhava他們的幫助。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/438107.html
上一篇:htaccess在404時重定向
