Reputation: 105
I have re-created a site from WordPress in native PHP, and have added certain ReWrite rules in the .htaccess to mimic the WP URL's, but I need to add a couple of reWrite rules to beautify a couple of URL's.
I tried creating 2 directories and having separate .htaccess files in each directory, but to no avail.
RewriteEngine On
RewriteBase /
# hide .php extension snippet
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1/ [R,L]
# add a trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/$
RewriteRule . %{REQUEST_URI}/ [L,R=301]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
#Beautify new URL's
RewriteRule ^operating-areas/([^/]+) town.php?place=$1
RewriteRule ^operating-area/([^/]+)/([^/]+) keyword.php?place=$1&keyword=$2
www.domain.com/operating-areas/London
needs to be sent to
myPage.php?place=London
and then
www.domain.com/operating-area/London/Finance
needs to be sent to
myOtherPage.php?place=London&keyword=Finance
At the moment I am getting 'Too many redirections'.
The rules I have used for the WordPress styling were found on the internet, hence my limited knowledge of this subject hinders me from stopping them firing in specific circumstances.
Upvotes: 1
Views: 23
Reputation: 785266
You can replace all of your code with this block:
RewriteEngine On
RewriteBase /
# hide .php extension snippet
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1/ [R,L]
# add a trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/$
RewriteRule . %{REQUEST_URI}/ [L,R=301]
#Beautify new URL's
RewriteRule ^operating-areas/([^/]+)/?$ town.php?place=$1 [L,QSA,NC]
RewriteRule ^operating-area/([^/]+)/([^/]+)/?$ keyword.php?place=$1&keyword=$2 [L,QSA,NC]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
/?$
at the end of the pattern allows matching of an optional /
at the end. It is also important to keep this rule before .php
adding rule.
Upvotes: 1