PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, February 4, 2022

[FIXED] .htaccess file is working but causes constant refreshing/redirects

 February 04, 2022     .htaccess, apache, lamp, php     No comments   

Issue

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.



Answered By - anubhava
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing