Forum

Thread tagged as: Question, Problem, Hosting

Code for .htaccess to remove .php from the url extension

I am having a bit of issues trying to exclude the .php on the url. I have used the following code in the .htaccess and it works, only to realize that it prevented my from accessing my login window.

Any suggestions? Better code that you may have etc. And also by doing this will this mean that I will have a double page hanging around the internet? ie. .php and a version without? Dont want to mess up the seo and get blacklisted for doubling up content etc.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ /$1 [R=301,L]
# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ /$1 [R=301,L]
# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [L]

M

Mark Chen

Mark Chen 0 points

  • 6 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

You should exclude the /perch folder from your rules.

RewriteRule ^perch - [L] 

I tried that and it didn't work. Still blocking me from seeing the login.

AddHandler php53 .php .html
Action php53 /cgi-bin/php53.cgi

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ /$1 [R=301,L]
# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ /$1 [R=301,L]
# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [L]
RewriteRule ^perch - [L]

Try putting RewriteRule ^perch - [L] as the first rule, right under RewriteEngine On (rules fire sequentially, from top to bottom).

Edit: I'd also highly recommend an article written by Drew on 24 Ways, URL Rewriting for the Fearful. It was a total game-changer for me. You'll find details about "Excluding a Directory" under the "Useful Snippets" heading.

Thanks, that did the trick.

One problem I am now encountering is that although I am able to log in, I am having issues with the assets folder. I am able to add items to the assets folder, but when I want to use the images I get a spinning wheel waiting for the assets to load. This only happens when the .htaccess code is present.

Any fixes?

RewriteEngine On
RewriteRule ^perch - [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ /$1 [R=301,L]
# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ /$1 [R=301,L]
# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [L]
Drew McLellan

Drew McLellan 2638 points
Perch Support

If you view the asset file directly, what happens?

From clicking on assets in the dashboard I can see the assets and it allows me to create new assets. It is when I go to a page and try to add an asset via the image content where the page slides over to the assets pane which remains white and has the turning wheel.

Drew McLellan

Drew McLellan 2638 points
Perch Support

Which part is white? The panel?

Yes the panels on the right that should have the tiles of assets to choose from. There is where lies the spinning wheel as if waiting for the images to populate the tiles so I can then select an asset. It however stays like that indefinitely.

Drew McLellan

Drew McLellan 2638 points
Perch Support

Are there any errors in your browser's console?

None at all. Only what I have described.

Drew McLellan

Drew McLellan 2638 points
Perch Support

Does the Network tab reveal anything? I really don't know what to suggest without some more information.

Not sure what you mean when you say network tab?

how about I make you log in and you can get a feel as to what I mean.

Drew McLellan

Drew McLellan 2638 points
Perch Support

I mean the network tab in your browser tools.