0
votes

I have spent count-less hours trying to develop a good .htaccess to offer extensionless urls for SEO purposes. Almost every modern website I see has extensionless URL's and being a fairly new site I want to take this approach.

I have been able to successfully convert the site, using .htaccess, to extensionless. For example:

example.com/page.php will become example.com/page1 in the URL

HOWEVER, existing pages that are the same location (such as contact.php page) will do a LOOP when doing a 301 redirect. So, for example, so that a search engine doesn't think /contact.php and /contact are two different pages, the existing indexed /contact.php has a 301 redirect to /contact

However, the /contact url pulls its data from /contact.php behind the scenes and .htaccess knows this and then wants to apply the 301 redirect again to the behind the scenes /contact.php to /contact causing a loop. I cannot figure out how to stop this.

Bottom line is that I want a 301 redirect from /contact.php to /contact with /contact in the URL reflecting the contents of /contact.php physical file.

Here is my .htaccess to works well except for pages that have 301 redirects.

I'm also interested in any other issues you may see in my .htaccess :)

.htaccess
    Options -MultiViews
    Options +FollowSymLinks
    RewriteEngine on
    RewriteBase /


    ## 301 Redirects
        # 301 Redirect 1
        RewriteCond %{QUERY_STRING}  ^$
        RewriteRule ^contact\.php$ /contact? [R=301,NE,NC,L]


    ## Exclude appropriate directories from rewrite rules  ^(blogsphere)
        RewriteRule ^(blogsphere) - [L]

    ## Unless directory, remove trailing slash
        # If requested URL ending with slash does not resolve to an existing directory
            RewriteCond %{REQUEST_FILENAME} !-d
        # Externally redirect to remove trailing slash
            RewriteRule ^(.+)/$ $1 [QSA,L]

    ## Redirect external .php requests to extensionless url
        # Any type of POST/GET should be a condition so that it doesnt rewrite without that data (end fileames in submissionscript)
            RewriteCond %{REQUEST_URI} !^/([a-z-]+)\-(submissionscript)(.*)$
        #Request has to end in .php
            RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
            RewriteRule ^(.+)\.php$ $1$2 [QSA,L]

    ## If it is not a directory then internal resolve request to end with .php for extensionless php urls (/page will internally resolve to /page.php)
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^((?!(\.|\.php)).)*$ $0.php [QSA,L]
1

1 Answers

0
votes

Have it like this:

Options -MultiViews
Options +FollowSymLinks
RewriteEngine on
RewriteBase /

# skip blogsphere directory for rewrite
RewriteRule ^(blogsphere) - [L]

## Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} \s(.+?)/+[?\s]
RewriteRule ^(.+?)/$ $1 [R=301,L]

# remove .php extension from URL - external redirect 
RewriteCond %{REQUEST_URI} !^/([a-z-]+)-(submissionscript)(.*)$
RewriteCond %{THE_REQUEST} \s/+(?:index)?(.*?)\.php[\s?] [NC]
RewriteRule ^ %1 [R=301,L,NE]

# internally rewrite a file to file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]