Home > Mobile >  Second rewriterule is not working in htaccess and shows 404 not found page
Second rewriterule is not working in htaccess and shows 404 not found page

Time:03-06

Hi I have links like this:

www.example.com/a-letter/a-1.html
www.example.com/b-letter/b-1.html

There are 3 files under the a letter and b-letter folder also. And i removed a-letter section with this .htaccess code:

##
RewriteCond %{THE_REQUEST} /a-letter/ [NC]
RewriteRule ^a-letter/(.*)$ /$1 [L,R=301,NC,NE]
##

And removed b-letter section with this .htaccess code:

##
RewriteCond %{THE_REQUEST} /b-letter/ [NC]
RewriteRule ^b-letter/(.*)$ /$1 [L,R=301,NC,NE]
##

I am redirecting all a-1 a-2 a-3 files with this htaccess code:

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]

Also i am redirecting all b-1 b-2 b-3 files with this htaccess code:

##
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##

------------------------------------------------------------------------

CodePudding user response:

Hi I solved my problem like this:

Firstly My website links was like this:

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

I removed a-letter from link directory with this code:

# remove /a-letter/ from URLs
RewriteCond %{THE_REQUEST} /a-letter/ [NC]
RewriteRule ^a-letter/(.*)$ /$1 [L,R=301,NC,NE]

and for b-letter for comments/a/ and comments/b/ like above only letters different.

I changed rewrite rule internally for /a-1 /a-2 /a-3 like this: (Moreover my hosting service use lite speed server because of this reason i wrote <IfModule LiteSpeed> intead of <ifModule mod_rewrite.c>) with this code www.example.com/a-1 displays www.example.com/a-1.html 's content internally. There is no change in the link.

<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>

Also i changed for b-letter and b-1 b-2 b-3 like above only letters different.

I rewrote non /comments/a/ links internally with this code:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/comments/a/$1.html -f
RewriteRule ^/?([^.?] )$/?$ comments/a/$1.html [L,NC]

and for non comments/b/ links like above only letters different.

My .htaccess file's contents, in order, are as follows:

##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##
.
.
.
------------------------------------------------------------------------

Every thing works fine with this codes and my website have full friendly-urls . Special thanks to the @arkascha and @anubhava for their helps.

  • Related