Home > Net >  How to redirect all subfiles to main link in link directory with htaccess
How to redirect all subfiles to main link in link directory with htaccess

Time:03-02

Hi i have link directories like this:

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

www.example.com/a-letter/a-2

www.example.com/a-letter/a-3

i removed a-letter folder name from the link with this code:

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

and i want to redirect all a-1 a-2 and a-3 files under the a-letter folder to like below with one htaccess code

www.example.com/a-1
www.example.com/a-2
www.example.com/a-3

because i am using this codes for each link and i want to use one code for this. How can i specify the code for all files

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^a-1$ /a-letter/a-1.html [L]
</IfModule>

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^a-2$ /a-letter/a-2.html [L]
</IfModule>

I will be grateful if you could help me.

CodePudding user response:

This probably is what you are looking for:

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-letter/$1.html [END]

An alternative would be that, if the name scheme is some thing like "a-.html":

RewriteEngine on

RewriteRule ^/?a-letter/(.*)(/|\.html)?$ /$1 [R=301,L]

RequestCond %{DOCUMENT_ROOT}/a-letter%{REQUEST_URI}.html -f
RewriteRule ^/?a-(\d )/? /a-letter/a-$1.html [END]

I personally do not use those <IfModule mod_rewrite.c> conditions ... Yes, the might formally prevent an internal server error. But what does that help?

  1. Operating your site without those rules getting applied most likely does not make much sense.
  2. You know whether you have installed the rewriting module or not. And you are most likely not going to change that frequently.
  • Related