Is it possible to check if a page exist in a folder by using .htaccess?

Discussion in 'Apache' started by Ian08, Aug 27, 2017.

  1. #1
    I'm currently not using a CMS for my website. I just create pages in .php format and put them onto my server.

    Assuming that I put my tutorial articles in the /tutorial folder and news articles in the /news folder. The original urls of those articles would look like:
    
    http://www.mysite.com/tutorial/the-title-of-a-tutorial-article.php
    http://www.mysite.com/news/the-title-of-a-news-article.php
    Code (markup):
    By using url rewriting, I made the urls look a bit more beautiful by removing file extension and adding trailing slash at the end:
    
    http://www.mysite.com/tutorial/the-title-of-a-tutorial-article/
    http://www.mysite.com/news/the-title-of-a-news-article/
    Code (markup):
    Here comes the question. What if I want to make the urls even more beautiful by removing the folder names? Here's how I want them to look like:
    
    http://www.mysite.com/the-title-of-a-tutorial-article/
    http://www.mysite.com/the-title-of-a-news-article/
    Code (markup):
    Is it possible to do that in .htaccess?

    Note that I also have some basic pages like About Us page and Contact Us pages in the root folder that share the same url format (http://www.mysite.com/about-us/, http://www.mysite.com/contact-us/, etc). And I also have folders such as /css and /js for storing my css and js files.

    If it can be done in .htaccess, I'm guessing the approach is checking if a requested page exists in the /tutorial, /news, or root folder. Once finding it, display it; otherwise redirect to the 404 page. But I have no ideal about how the code should be wrote. Therefore some advices would be much appreciated. And I hope the approach will not slow down the server response time noticeably.
     
    Last edited: Aug 27, 2017
    Ian08, Aug 27, 2017 IP
  2. RoseHosting

    RoseHosting Well-Known Member

    Messages:
    230
    Likes Received:
    11
    Best Answers:
    11
    Trophy Points:
    138
    #2
    To remove a directory from URL you can use the following htaccess rule:
    RewriteEngine On
    RewriteRule ^folder_to_remove/(.*)$ /$1 [L,R=301]
    
    Code (markup):
     
    RoseHosting, Aug 28, 2017 IP
  3. Ian08

    Ian08 Well-Known Member

    Messages:
    93
    Likes Received:
    4
    Best Answers:
    1
    Trophy Points:
    108
    #3
    @RoseHosting,

    Thanks. Before I added that new rewrite rule, I tested linking to http://www.mysite.com/folder_to_remove/test.php and http://www.mysite.com/folder_to_remove/test/, and the page showed up correctly.

    But after I added that new rewrite rule, the above two urls are all redirecting to http://www.mysite.com/test.php with the 404 page showing up.

    Here is my .htaccess:
    RewriteEngine On
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
    RewriteRule (.*)$ /$1/ [R=301,L]
    
    RewriteCond %{DOCUMENT_ROOT}/$1.php -f
    RewriteRule ^([^/]+)/$ $1.php [L]
    
    RewriteCond %{DOCUMENT_ROOT}/$1/$2.php -f
    RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php [L]
    
    RewriteRule ^folder_to_remove/(.*)$ /$1 [L,R=301]
    HTML:
    Not sure where I am doing wrong.
     
    Ian08, Aug 28, 2017 IP