SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Mar 2012
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    htaccess extensionless url question

    Hi All,

    I have a few questions Im hoping someone can answer

    Ive written an htaccess file that removes the trailing slash from html files. This all works good. Am I doing this right? Also do I have to write "RewriteEngine on" twice based on the following code or is that overkill?

    Any recommendations on adding or taking away anything from this file?

    My whole goal is to write nice looking urls and avoid duplicate content to which I read that with the trailing slash and without can be treated as two different files.

    My file looks like this.......

    ErrorDocument 404 /404page.html
    RewriteEngine on
    RewriteCond %{HTTP_HOST} !^www.mydomain.com$
    RewriteRule ^(.*)$ http://www.mydomain.com/$1 [R=301,L]
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.html\ HTTP/
    RewriteRule ^index\.html$ http://www.mydomain.com/ [R=301,L]

    Options +FollowSymLinks
    RewriteEngine On

    # Remove trailing slashes.
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.+)/$ /$1 [R=permanent,QSA]

    # Redirect to HTML if it exists.
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}.html -f
    RewriteRule ^(.+)$ $1.html [L,QSA]

    Ive ran a test on the and posted the following files

    www.mydomain.com/test/index.html - which goes to www.mydomain.com/test/ (note the trailing slash)
    www.mydomain.com/test/test1.html - which goes to www.mydomain.com/test/test1 (note the NO trailing slash)
    www.mydomain.com/test/test2.html - which goes to www.mydomain.com/test/test2 (note the NO trailing slash)

    Is this how its supposed to work?


    Thanks in advance.

  2. #2
    SitePoint Wizard
    Join Date
    Dec 2003
    Location
    USA
    Posts
    2,582
    Mentioned
    29 Post(s)
    Tagged
    0 Thread(s)
    Yes, you can remove the second RewriteEngine ON.

    Also, I would probably move the Options +FollowSymLinks to the sites-enabled file instead, but that's optional.

    Otherwise I don't see anything that shouldn't really be there.

    Though you should note that if you have a file.html and directory with the same name, it'll be impossible to reach the file through this method. As long as that's okay, you should be good.

  3. #3
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,604
    Mentioned
    19 Post(s)
    Tagged
    2 Thread(s)
    wf,

    First, please use the [code] ... [/code] to wrap your code.

    Quote Originally Posted by wickfree View Post
    ... do I have to write "RewriteEngine on" twice based on the following code or is that overkill?

    Overkill - but not critical.

    Any recommendations on adding or taking away anything from this file?

    Code:
    ErrorDocument 404 /404page.html
    RewriteEngine on
    RewriteCond %{HTTP_HOST} !^www.mydomain.com$
    RewriteRule ^(.*)$ http://www.mydomain.com/$1 [R=301,L]
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.html\ HTTP/
    RewriteRule ^index\.html$ http://www.mydomain.com/ [R=301,L] 
    
    Options +FollowSymLinks # Move before FIRST RewriteEngine on
    RewriteEngine On # Remove duplicate
    
    # Remove trailing slashes.
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.+)/$ /$1 [R=permanent,QSA] 
    # QSA is not required (because you're not creating a new query string) but a Last flag is required (and is missing)
    
    # Redirect to HTML if it exists.
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}.html -f
    RewriteRule ^(.+)$ $1.html [L,QSA]
    # Same comment on QSA
    Excellent - most people would not have remembered to check that the file with .html exists!
    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •