The Community Forums

Interact with an entire community of cPanel & WHM users!
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

.htaccess extentionless url rewrites breaking folder redirects?

Discussion in 'Workarounds and Optimization' started by Jase Wolf, Sep 9, 2016.

  1. Jase Wolf

    Jase Wolf Registered

    Joined:
    Sep 9, 2016
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    United Kingdom
    cPanel Access Level:
    Reseller Owner
    Hi, on my sites, I'm wanting extentions to be removed, and then any extentioned requests get rewritten, also removing the trailing slash.

    By doing this, people can land on the page they want whether they put html, php, htm, or shtml (if there's more common extentions for web pages will be good to know) or no extention at all.

    Trouble is, folder/file redirects work finebefore I place in the htaccess rules I have put together, but after, nothing I do works.

    Could anyone work out what is the culprit and what I can do instead to have the same effect but with working redirects?

    I asked this on my hosting provider's forums InstaFree.com, and one suggested it could be the ordering is wrong? He suggested to put redirects above these rules. Trouble is though, Cpanel redirect tool places redirect entries on the bottom, so that'd not be a solution.

    Also asked this on two other forums but just having no response. Apologies if this is in the wrong section but does seem the most appropriate

    Here's the code. Thanks, Jase Wolf

    Code:
    # Rewrite urls to remove trailing slash
    
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} /(.*)/$
    RewriteRule ^ /%1 [R=301,L]
    
    
    # Redirect http to https
    
    RewriteCond %{HTTPS} off
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    
    
    # Rewrite files to extensionless urls
    
    RewriteRule ^([^/.]+)$ /$1.php [L]
    RewriteRule ^([^/.]+)$ /$1.html [L]
    RewriteRule ^([^/.]+)$ /$1.shtml [L]
    RewriteRule ^([^/.]+)$ /$1.htm [L]
    
    
    # Redirect external requests to extensionless urls
    
    RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
    RewriteRule ^(.+)\.php$ $1 [R=301,L]
    RewriteCond %{THE_REQUEST} ^(.+)\.html([#?][^\ ]*)?\ HTTP/
    RewriteRule ^(.+)\.html$ $1 [R=301,L]
    RewriteCond %{THE_REQUEST} ^(.+)\.htm([#?][^\ ]*)?\ HTTP/
    RewriteRule ^(.+)\.htm$ $1 [R=301,L]
    RewriteCond %{THE_REQUEST} ^(.+)\.shtml([#?][^\ ]*)?\ HTTP/
    RewriteRule ^(.+)\.shtml$ $1 [R=301,L]
     
  2. Jase Wolf

    Jase Wolf Registered

    Joined:
    Sep 9, 2016
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    United Kingdom
    cPanel Access Level:
    Reseller Owner
    Right after some testing this is the culprit, had a bit of a feeling it was this as I was also having redirect issues just with the php rewriting I were using.

    Code:
    # Rewrite files to extensionless urls
    
    RewriteRule ^([^/.]+)$ /$1.php [L]
    RewriteRule ^([^/.]+)$ /$1.html [L]
    RewriteRule ^([^/.]+)$ /$1.shtml [L]
    RewriteRule ^([^/.]+)$ /$1.htm [L]
    Using this code instead fixes it

    Code:
    # Rewrite files to extensionless urls
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule ^(.*)$ $1.php [NC,L]
    RewriteCond %{REQUEST_FILENAME}.html -f
    RewriteRule ^(.*)$ $1.html[NC,L]
    RewriteCond %{REQUEST_FILENAME}.htm -f
    RewriteRule ^(.*)$ $1.htm [NC,L]
    RewriteCond %{REQUEST_FILENAME}.shtml -f
    RewriteRule ^(.*)$ $1.shtml [NC,L]
    So WOOOHOOOOOOOOOOOOOOOO, four forums I posted this in, been trying to work this out for ages and finally work it out lol
     
  3. cPanelMichael

    cPanelMichael Forums Analyst
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    30,678
    Likes Received:
    654
    Trophy Points:
    113
    cPanel Access Level:
    Root Administrator
Loading...

Share This Page