1

I'm still working my way round htaccess and would like some input on whether my code is correct or needs some work. Also I know there are tonnes of questions like this but still can't figure it out, how do I rewrite the URL days/content.php?day=mon to days/mon or days/content/mon? Thanks!

#---------------------------------------------------------
#rewrite engine + rewritecond
#---------------------------------------------------------
<IfModule mod_rewrite.c>
  # enable the rewrite engine
  Options +FollowSymlinks
  RewriteEngine On

  # Set your root directory
  RewriteBase /

  # To externally redirect /dir/foo.php to /dir/foo
  RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
  RewriteRule ^ %1 [R,L,NC]

  ## To internally redirect /dir/foo to /dir/foo.php
  RewriteCond %{REQUEST_FILENAME}.php -f [NC]
  RewriteRule ^ %{REQUEST_URI}.php [L]

  # remove index and reference the directory
  RewriteRule (.*)/index$ $1/ [R=301]

  # remove trailing slash if not a directory
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_URI} /$
  RewriteRule (.*)/ $1 [R=301]

  #prevent hotlinking
  RewriteCond %{HTTP_REFERER} !^$
  RewriteCond %{HTTP_REFERER} !^https?://(.+\.)?dundaah.com [NC]
  RewriteRule \.(jpe?g|png|gif|bmp|css|js|php|xml)$ - [NC,F,L]

  #deny access to hidden files and directories
  RewriteCond %{SCRIPT_FILENAME} -d [OR]
  RewriteCond %{SCRIPT_FILENAME} -f
  RewriteRule "(^|/)\." - [F]

</IfModule>

#---------------------------------------------------------
#compress text files
#---------------------------------------------------------
<IfModule mod_deflate.c>

    # Force compression for mangled headers.
    <IfModule mod_setenvif.c>
        <IfModule mod_headers.c>
            SetEnvIfNoCase ^(Accept-EncodXng|X-cept-Encoding|X{15}|~{15}|-{15})$ ^((gzip|deflate)\s*,?\s*)+|[X~-]{4,13}$ HAVE_Accept-Encoding
            RequestHeader append Accept-Encoding "gzip,deflate" env=HAVE_Accept-Encoding
        </IfModule>
    </IfModule>

    # Compress all output labeled with one of the following MIME-types
    <IfModule mod_filter.c>
        AddOutputFilterByType DEFLATE application/atom+xml \
                                      application/javascript \
                                      application/json \
                                      application/rss+xml \
                                      application/vnd.ms-fontobject \
                                      application/x-font-ttf \
                                      application/x-web-app-manifest+json \
                                      application/xhtml+xml \
                                      application/xml \
                                      font/opentype \
                                      image/svg+xml \
                                      image/x-icon \
                                      text/css \
                                      text/html \
                                      text/plain \
                                      text/x-component \
                                      text/xml
    </IfModule>

</IfModule>

#---------------------------------------------------------
#compress files with mod_gzip
#---------------------------------------------------------
<IfModule mod_gzip.c>
    mod_gzip_on       Yes
    mod_gzip_dechunk  Yes
    mod_gzip_item_include file      \.(html?|txt|css|js|php|pl)$
    mod_gzip_item_include handler   ^cgi-script$
    mod_gzip_item_include mime      ^text/.*
    mod_gzip_item_include mime      ^application/x-javascript.*
    mod_gzip_item_exclude mime      ^image/.*
    mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</IfModule>
 
#---------------------------------------------------------
#set expires headers cache control 
#---------------------------------------------------------
<IfModule mod_expires.c>
    ExpiresActive on
    ExpiresDefault                                      "access plus 1 week"
#CSS
    ExpiresByType text/css                              "access plus 1 week"
#Data interchange
    ExpiresByType application/json                      "access plus 0 seconds"
    ExpiresByType application/xml                       "access plus 0 seconds"
    ExpiresByType text/xml                              "access plus 0 seconds"
#Favicon (cannot be renamed!)
    ExpiresByType image/x-icon                          "access plus 1 week"
#HTML components (HTCs)
    ExpiresByType text/x-component                      "access plus 1 week"
#HTML
    ExpiresByType text/html                             "access plus 0 seconds"
#JavaScript
    ExpiresByType application/javascript                "access plus 1 week"
#Manifest files
    ExpiresByType application/x-web-app-manifest+json   "access plus 0 seconds"
    ExpiresByType text/cache-manifest                   "access plus 0 seconds"
#Media
    ExpiresByType audio/ogg                             "access plus 1 week"
    ExpiresByType image/gif                             "access plus 1 week"
    ExpiresByType image/jpeg                            "access plus 1 week"
    ExpiresByType image/jpg                             "access plus 1 week"
    ExpiresByType image/png                             "access plus 1 week"
    ExpiresByType video/mp4                             "access plus 1 week"
    ExpiresByType video/ogg                             "access plus 1 week"
    ExpiresByType video/webm                            "access plus 1 week"
#Web feeds
    ExpiresByType application/atom+xml                  "access plus 1 hour"
    ExpiresByType application/rss+xml                   "access plus 1 hour"
#Web fonts
    ExpiresByType application/font-woff2                "access plus 1 month"
    ExpiresByType application/font-woff                 "access plus 1 month"
    ExpiresByType application/vnd.ms-fontobject         "access plus 1 month"
    ExpiresByType application/x-font-ttf                "access plus 1 month"
    ExpiresByType font/opentype                         "access plus 1 month"
    ExpiresByType image/svg+xml                         "access plus 1 month"
</IfModule>
 
#---------------------------------------------------------
#cache files
#---------------------------------------------------------
<ifModule mod_headers.c>
    <filesMatch ".(ico|jpe?g|png|gif|swf)$">
        Header set Cache-Control "public"
    </filesMatch>

    <filesMatch ".(css)$">
        Header set Cache-Control "public"
    </filesMatch>

    <filesMatch ".(js)$">
        Header set Cache-Control "private"
    </filesMatch>

    <filesMatch ".(x?html?|php)$">
        Header set Cache-Control "private, must-revalidate"
    </filesMatch>
    
    #to disable for certain file type
    <FilesMatch ".(pl|php|cgi|spl|scgi|fcgi)$">
        Header unset Cache-Control
    </FilesMatch>
</ifModule>

#---------------------------------------------------------
#turn e-tags off
#---------------------------------------------------------
<IfModule mod_headers.c>
    Header unset ETag
</IfModule>
FileETag None

#---------------------------------------------------------
#disable dir browsing + script execution
#---------------------------------------------------------
Options -Indexes -ExecCGI
AddHandler cgi-script .php .pl .py .jsp .asp .htm .shtml .sh .cgi

#---------------------------------------------------------
#block access to your .htaccess file + more
#---------------------------------------------------------
<Files .htaccess>
  order allow,deny
  deny from all
</Files>

#---------------------------------------------------------
#create custom error pages
#---------------------------------------------------------
ErrorDocument 400 /errors/400.php
ErrorDocument 401 /errors/401.php
ErrorDocument 403 /errors/403.php
ErrorDocument 404 /errors/404.php
ErrorDocument 500 /errors/500.php

#---------------------------------------------------------
#display no php errors to user
#---------------------------------------------------------
php_flag display_startup_errors off
php_flag display_errors off
php_flag html_errors off

#---------------------------------------------------------
# log php errors to file
#---------------------------------------------------------
php_flag log_errors on
#php_value error_log /location/to/php_error.log
Max Njoroge
  • 489
  • 3
  • 21

3 Answers3

1

If you're looking to rewrite the URL days/content.php?day=mon to days/mon, consider the following rewriting rules:

RewriteEngine On
RewriteCond %{REQUEST_URI} ^/days/content.php
RewriteCond %{QUERY_STRING} ^day=([a-zA-Z]+)$
RewriteRule ^(.*)$ /days/%1? [R,L]

or if you're looking to rewrite it to days/content/mon, consider this one:

RewriteEngine On
RewriteCond %{REQUEST_URI} ^/days/content.php
RewriteCond %{QUERY_STRING} ^day=([a-zA-Z]+)$
RewriteRule ^(.*)$ /days/content/%1? [R,L]

Hope it helps!

Wolverine
  • 1,712
  • 1
  • 15
  • 18
1

All you need to use is this:

RewriteEngine On
RewriteRule ^days/([^/]*)$ /days/content.php?day=$1 [L]

It will leave you with the URL: www.example.com/days/mon.

Make sure you clear your cache before testing it.

Joe
  • 4,877
  • 5
  • 30
  • 51
  • thanks, but still not working, the url in the browser remains as `days/content.php?day=mon` but i get a 500 error – Max Njoroge Jan 23 '17 at 05:27
0
RewriteCond %{SCRIPT_FILENAME} !-f    
RewriteRule ^days\/(.+)$ days/content.php?day=$1 [NC,L]

will redirect from days/mon to days/content.php?day=mon.

If you are getting a not found error, it may be because of the RewriteBase /. By default, .htaccess urls are relative to the directory it is in. By setting the base to /, the urls then become relative to root instead.

For the example below, I have removed RewriteBase:

<IfModule mod_rewrite.c>
    Options +FollowSymlinks
    RewriteEngine On    

    # This shouldn't be required, as it defaults to the directory the .htaccess is in
    # RewriteBase /

    # rewrite days/[day] into days/content.php?day=[day]
    RewriteCond %{SCRIPT_FILENAME} !-f
    RewriteRule ^days\/(.+)$ days/content.php?day=$1 [NC,L]

    ...
</IfModule>
tklg
  • 2,572
  • 2
  • 19
  • 24