skip to Main Content

I’ve search the internet and Stackoverflow for hours now and I just can’t fix my problem completely. I already figured it out years ago, but lost the file and I can’t reconstruct it anymore. So I’m hoping for your help!

I’ve HTML-URLs and I want to simulate folders AND I also want to remove the extension .html

INPUT: domain.com/file.html
OUTPUT: domain.com/file/

INPUT: domain.com/folder1_folder2_file.html
OUTPUT: domain.com/folder1/folder2/file/

I’m also not so sure about the flags at the end of the line.

Notworking code so far:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1 [R=301,L]
RewriteRule ^([^_]*)_(.*)$ $1/$2 [R=301,L]
RewriteRule ^([^_]*)?_([^_]*)?_(.*)$ $1/$2/$3 [R=301,L]

Thanks for your help.

2

Answers


  1. Chosen as BEST ANSWER

    Finally I have figured it out myself. It rewrites URLs with non-digit characters and dashs.

    For anyone interested:

    RewriteEngine On
    RewriteRule ^/?([a-zA-Z-]+)$ $1.html [N]
    RewriteRule ^/?([a-zA-Z-]+)/([a-zA-Z-]+)/([a-zA-Z-]+)$ $1_$2_$3.html [L]
    

    Nevertheless, thank you for your efforts =) @RavinderSingh13


  2. Could you please try following.

    RewriteEngine ON
    RewriteCond %{REQUEST_URI} _
    RewriteCond %{REQUEST_URI} .html/?$
    RewriteCond %{THE_REQUEST} /([^_]*)_([^_]*)_([^.]*).htmls [NC]
    RewriteRule ^(.*)$ /%1/%2/%3/ [R=301,NC,L]
    
    RewriteCond %{REQUEST_URI} !_
    RewriteCond %{REQUEST_URI} .html/?$
    RewriteCond %{THE_REQUEST} /([^.]*).htmls [NC]
    RewriteRule ^(.*)$ /%1/ [R=301,NC,L]
    

    Testing with curl command:

    curl -IL "http://localhost:80/file.html"
    HTTP/1.1 301 Moved Permanently
    Server: Apache/2.4.46 (Win64) OpenSSL/1.1.1g PHP/7.4.11
    Location: http://localhost/file/
    
    curl -IL "http://localhost:80/folder1_folder2_file.html"
    HTTP/1.1 301 Moved Permanently
    Server: Apache/2.4.46 (Win64) OpenSSL/1.1.1g PHP/7.4.11
    Location: http://localhost/folder1/folder2/file/
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search