0

I'm working on my .htaccess to obtain an extensionless site. I've searched online and implemented some rules that are working, but when I type in a url that doesn't exist (e.g. mysite.com/dsfadfasdf ) it starts to go in an infinite loop.

If anyone would be so kind as to help me get rid of the loop and perhaps redirect to a page of my choosing (where I'll say the page doesn't exist) I'd really appreciate it!

Options +FollowSymLinks 
Options +Indexes 
RewriteEngine On 
RewriteBase /

# Redirect www to non www
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

# Home page language
RewriteRule ^(en|ro)/?$ index.php?lang=$1 [L]

# For the profile page
RewriteRule ^(.+)\/accommodation-(.+)\/([0-9]+)\/(.+)$ location.php?id=$3&town=$2&hname=$4&lang=$1 [NC,L]

# Internally rewrite extensionless URL to corresponding .php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*)$ $1.php?%1 [NC,L,QSA]

# Externally redirect (only) direct client requests for .php URLs to extensionless URLs:
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*[^.#?\ ]+\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(([^/]+/)*[^.]+)\.php http://mysite.com/$1 [R=301,L] 

I believe the issue is in the last rule. Any suggestions?

  • 1
    Disable all the rules, and activate 1 by 1 to see exactly where it messes up your site. Also I believe your problem is at `# Internally rewrite extensionless URL to corresponding .php` – Prix Jul 20 '13 at 19:50
  • Then http://stackoverflow.com/questions/2363520/redirecting-404-error-with-htaccess – l2aelba Jul 20 '13 at 19:51

1 Answers1

0

.htaccess keeps rewriting if the url changes. Force it to stop rewriting after you've rewritten your url internally with the END flag:

# Internally rewrite extensionless URL to corresponding .php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*)$ $1.php?%1 [NC,END,QSA]

Documentation is here.

Sumurai8
  • 20,333
  • 11
  • 66
  • 100