Search code examples
phpapache.htaccesslamp

.htaccess file is working but causes constant refreshing/redirects


I have changed my .htaccess based on a tutorial to hide the .php file extension and $_Get from URL but the page keeps on jumping.

I have tried a number of .htaccess changes this is the only one that is even partially working. However, it seems to be causing constant redirects/refreshes.

RewriteEngine on

RewriteBase /

RewriteCond %{HTTP:X-Forwarded-Proto} =http

RewriteRule .* https://%{HTTP:Host}%{REQUEST_URI} [L,R=permanent]

RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{REQUEST_FILENAME}\.php -f

RewriteRule ^(.*)/([0-9a-zA-Z_-]+) $1.php?name=$2 [NC,L]

Looking to change "profile.php?name=company-name" to "profile/company-name" without the page looking or functioning any differently. Some of the $_Get dynamic content is loading but the entire page keeps on skipping and none of the CSS styles are working.


Solution

  • Replace all of your rules with this:

    RewriteEngine on    
    RewriteBase /
    
    RewriteCond %{HTTPS} !on
    RewriteCond %{HTTP:X-Forwarded-Proto} =http
    RewriteRule ^ https://%{HTTP:Host}%{REQUEST_URI} [L,R=301,NE]
    
    RewriteCond %{DOCUMENT_ROOT}/$1.php -f
    RewriteRule ^([^/]+)/?$ $1.php [L]
    
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{DOCUMENT_ROOT}/$1.php -f
    RewriteRule ^([^/]+)/([\w-]+)/?$ $1.php?name=$2 [QSA,L]
    

    Make sure to test this change in a new browser to avoid old browser cache.

    For your css/js/issues add this just below <head> tag of your page's HTML:

    <base href="/" />
    

    so that every relative URL is resolved from that base URL and not from the current page's URL.