HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

.htaccess using public folder and SEO friendly URL

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
publichtaccessseofolderusingandurlfriendly

Problem

.htaccess is not my strong suit. Is there any there can optimize this code?

RewriteEngine on
RewriteCond %{REQUEST_URI} !^/(public)
RewriteRule (.*) /public/$1

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]

ErrorDocument 500 "Sorry, our script crashed. Oh dear"
ErrorDocument 500 /cgi-bin/crash-recover
ErrorDocument 500 http://error.example.com/server_error.html
ErrorDocument 404 /errors/not_found.html
ErrorDocument 401 /subscription/how_to_subscribe.html


don't know if it necessary but here are an example of the folder system

  • app



  • public



  • index.php



  • contact.php



  • forum



  • last_news.php



  • users



  • user.php



  • ask.php



  • .htaccess

Solution


  • Redundant RewriteEngine declarations



  • Unused match group in (public)



  • Escaping (using \) is not needed in the new URLs



  • A personal touch, but I prefer to keep the core directives at the top, and put module specific directives inside ` body, so that I won't receive errors if the modules are disabled.



  • Multiple declarations for ErrorDocument 500`



Options +FollowSymLinks -MultiViews

ErrorDocument 401 /subscription/how_to_subscribe.html# Turn mod_rewrite on
ErrorDocument 404 /errors/not_found.html
ErrorDocument 500 http://error.example.com/server_error.html

    RewriteEngine on
    RewriteBase /

    RewriteCond %{REQUEST_URI} !^/(public)
    RewriteRule ^ /%1%{REQUEST_URI}

    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteCond %{REQUEST_URI} !/$
    RewriteRule ^(.*)$ $1.php [L]

Code Snippets

Options +FollowSymLinks -MultiViews

ErrorDocument 401 /subscription/how_to_subscribe.html# Turn mod_rewrite on
ErrorDocument 404 /errors/not_found.html
ErrorDocument 500 http://error.example.com/server_error.html

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteBase /

    RewriteCond %{REQUEST_URI} !^/(public)
    RewriteRule ^ /%1%{REQUEST_URI}

    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteCond %{REQUEST_URI} !/$
    RewriteRule ^(.*)$ $1.php [L]
</IfModule>

Context

StackExchange Code Review Q#157517, answer score: 2

Revisions (0)

No revisions yet.