Search code examples
.htaccessmod-rewriteapache2

mod_rewrite rule is not working but I have the correct syntax


So my mod_rewrite rule isn't working to specify a specific php file for a URL.

Options -Indexes
php_value short_open_tag 1

#Caching
# 1 HOUR
<FilesMatch "\.(ico|pdf|flv)$">
Header set Cache-Control "max-age=31536000, public"
</FilesMatch>

# 1 HOUR
<FilesMatch "\.(jpg|jpeg|png|gif|swf)$">
Header set Cache-Control "max-age=31536000, public"
</FilesMatch>

# 1 HOUR
<FilesMatch "\.(xml|txt|css|js)$">
Header set Cache-Control "max-age=31536000, proxy-revalidate"
</FilesMatch>


<IfModule mod_rewrite.c>
#Rewrite rules
RewriteEngine on

RewriteRule ^about\/special-offers\/?$ about/special-offers.php


# remove www. from all domains
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [L,R=301]

# Add trailing slash
# TODO: This breaks POST data when forwarding to a URL without an ending slash
#RewriteCond %{REQUEST_URI}  !\.(php|html?|gif|jpe?g|JPE?G|png|bmp|js|swf|css|ico|xml|txt|woff2|woff)$
#RewriteRule ^(.*)([^/])$ http://%{HTTP_HOST}/$1$2/ [P,R=301]

# hide .svn folders
RewriteRule ^(.*/)*\.svn/ / [NC,L]

# send all images and misc files to the actual location
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
RewriteRule \.(gif|jpe?g|JPE?G|png|bmp|js|swf|ico|xml|txt) index.php [NC,L]

# send folder to the relevant php file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.php
RewriteCond %{REQUEST_FILENAME} !-f
#RewriteRule ^([^/]+)/([^/]+)/([^/]+)/$ $1/$2.php
RewriteRule ^([^/]+)\/([^/]+)\/ $1/$2.php

#RewriteRule ^([^/]+)/([^/]+)/$ $1/$2.php
RewriteRule ^([^/]+)/([^/]+)/$ $1/default.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]

# otherwise if the file or directory is not found, process through the index
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]
</IfModule>

I have a multitude of other rules in here, I can post them if need be, but since this is a specific rule I don't know why one of them would be overriding it. Direction is appreciated.

I've tried putting it at the top or bottom of my mod_rewrite section but neither seems to have any affect.

Something I've just noticed is that when I go to /about/special-offers/x (where x is any string), it loads the correct page.

Other


Solution

  • Finally fixed this issue, I did it by marking the RewriteRule as final with [L].

    Relevant portion:

    # send folder to the relevant php file
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^([^/]+)/$ $1.php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^([^/]+)\/([^/]+)\/ $1/$2.php [L]