Search code examples
.htaccesshttp-redirect

How to redirect all subfiles to main link in link directory with htaccess


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.


Solution

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

    UPDATE:

    In the comments you asked another question (again: please open a separate question in future for separate questions...). I would suggest such implementation:

    RewriteEngine on 
    
    RewriteRule ^/?a-letter/(.*)(/|\.html)?$ /$1 [R=301,L]
    RewriteRule ^/?comments/a/(.*)(/|\.html)?$ /$1 [R=301,L] 
    
    RequestCond %{DOCUMENT_ROOT}/a-letter%{REQUEST_URI}.html -f
    RewriteRule ^/?a-(\d+)/? /a-letter/a-$1.html [END]
    
    RequestCond %{DOCUMENT_ROOT}/comments/a%{REQUEST_URI}.html -f 
    RewriteRule ^/?(.*)/? /comments/a/$1.html [END]
    

    Your directives look fine, it might be that the issue you ran into is the order of directives. Always keep in mind that the directives are processed from top to bottom. Your last RewriteRule is very generic, it will match anything . Always keep more generic rules at the bottom, more precise exceptions further atop. And always try to modify rules such that they are less generic:

    So instead of

    RewriteRule ^/?(.*)/? /comments/a/$1.html [END]
    

    maybe something like that

    RewriteRule ^/?(\w*)/? /comments/a/$1.html [END]
    

    is possible?