.php .htaccess rewrite on a homestead/laravel project - php

I've read many tutorials on rewriting the /page.php to /page but I cannot get this to work on my laravel website..
# do not allow anyone else to read your .htaccess file
<Files .htaccess>
deny from all
</Files>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
The url doesn't rewrite using this content in the .htaccess file. Am I making a schoolboy error?
In fact, the page never really finishes loading!

Related

unable to hide .php extension. It worked only if i typed manually in url, How to fix it with .htaccess

My problem is that when I type the URL without .php extension (like as https://example.com/register) it worked, but when I click register link it redirect to https://example.com/register.php. I am trying to edit .htaccess file so that by default when I click register link URL should be without .php extension.
My go daddy .htaccess file code is
Options +MultiViews
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Try this htaccess
RewriteEngine On
# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://example.com/folder/$1 [R=301,L]
# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://example.com/folder/$1 [R=301,L]
# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [L]
Use another if not work
RewriteEngine On
# remove the index file
RewriteCond %{THE_REQUEST} ^.*/index
RewriteRule ^(.*)index$ http://www.example.org/$1 [R=301,L]
# remove the .php extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(([^/]+/)*[^.]+)$ $1.php [L]
# redirect from .php to less php
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*[^.#?\ ]+\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(([^/]+/)*[^.]+)\.php http://www.example.org/$1 [R=301,L]
apply this to your htaccess file
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^.]+)$ $1.php [NC,L]
it works for all(.php or no .php)
Options +MultiViews
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
You've enabled MultiViews (part of mod_negotiation) and you are also trying to append the file extension using mod_rewrite. You should do one or the other, not both. They both do essentially the same thing. MultiViews is probably "Winning", or worse, creating a conflict.
Your existing code is essentially the same as:
Options +MultiViews
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Thanks to all for contribution, and being ashamed myself not to reply your valuable efforts because my boss was asign me a different project.
working code is
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Additionally also remove .php extention from
Example all anchor tag of the page.
you can check here https://doxdocs.com/

Cut folder path from URL via .htaccess

I'm trying to shorten my URL but sadly can't find anything that helps.
I divide my code in folders. Index is positioned at root just like my .htaccess.
The folders are named like the file extensions, so php, js, css [...]
I have a link like the following:
localhost/php/getBets.php
and want it to be
localhost/getBets/
I already have the part that cut's the .php extension at the end, so here is my full .htacces
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
# Hide Index
IndexIgnore *
# Forbid accessing certain sites
RedirectMatch 403 ^/.gitignore$
RedirectMatch 403 ^/.htaccess$
RewriteRule ^(?!index)(?!.*getBets).*\.(php|rb|py|txt|md|sql|inc)$ - [F,L,NC]
# Hide .php file ending in URL
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
Can someone maybe tell me how I could achieve this? :)
Thanks alot!
For your required url you can use below rule in root directory it is for rewriting,
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/$ php/$1.php [L]
Actualy Apache still does not have pathinfo($,PATHINFO_DIRNAME), function like has PHP.
Use %{REQUEST_URI}, like this example:
RewriteRule ^(.+)/$ /path-dirname/$1 [R=301,L]
may reset with:
RewriteCond %{REQUEST_URI} ^(.+)/$
RewriteRule ^.+/$ %1 [R=301,L]

.htaccess redirect sub-directory to root issue

My main domain is tied to my public_html folder but I use an .htaccess redirect to change the root directory to a sub-directory in the folder to make it easier to host multiple sites. Example:
public_html
-- .htaccess
-- site1
---- .htaccess
-- site2
-- site3
The .htaccess file in the root directory is as follows:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteCond %{REQUEST_URI} !^/subdirectory/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /subdirectory/$1
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteRule ^(/)?$ subdirectory/ [L]
This works fine, but I think there are issues with this and the .htaccess file in my site1 directory. If I enter the URL it sometimes redirects to example.com/site1. So the website appears exactly the same on example.com and example.com/site1 but I don't want it to appear as example.com/site1. The second .htaccess file inside the site1 directory is show below.
RewriteEngine on
# Change default directory page
DirectoryIndex /home/
# Remove .php extension
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.php
RewriteRule (.*)\.php$ /$1/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/(.+)/$
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^(.*)/$ $1.php [L]
# Ensure all directory URLs have a trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\/$
RewriteCond %{REQUEST_URI} !\/[^\/]*\.[^\/]+$
RewriteRule .* http://%{HTTP_HOST}%{REQUEST_URI}/ [L,R=301]
# Same for HTTPS
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\/$
RewriteCond %{REQUEST_URI} !\/[^\/]*\.[^\/]+$
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI}/ [L,R=301]
# Redirect default error page to home page
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(/|error/)?$ /home/ [R=301,L]
# Error Documents
ErrorDocument 400 /error/?e=400
ErrorDocument 401 /error/?e=401
ErrorDocument 403 /error/?e=403
ErrorDocument 404 /error/?e=404
ErrorDocument 500 /error/?e=500
# Prevent viewing of htaccess file
<Files .htaccess>
order allow,deny
deny from all
</Files>
I'm not sure if it's an issue with the various rewrite rules I'm using that affects the URL, but I would appreciate any help if it can be resolved.
Thanks in advance and I apologise if my description isn't great.
Try
# Ensure all directory URLs have a trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\/$
RewriteCond %{REQUEST_URI} !\/[^\/]*\.[^\/]+$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1/ [QSA,L,R=301]
# Same for HTTPS
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\/$
RewriteCond %{REQUEST_URI} !\/[^\/]*\.[^\/]+$
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [QSA,L,R=301]
It works for me. %{REQUEST_URI} contains site1, so don't use it.
QSA adds any URL parameters like ?param1=val1&param2=val2 If you don't use them, you may omit QSA.
Also while testing replace R=301 to R=302 (temporary redirect), else your browser remembers the rule and doesn't send request again.

Rewriting .php to .html and redirecting .php to a 404 in .htaccess

I have a lot going on here in my .htaccess, and there's one more redirect I'd like to add, and I'm not sure how to do it.
All .php files are seen as .html, and if an actual .html file exists of the same name, that should be accessible. Everything is also routed through my main controller script, mycontroller.php. That's all working perfectly.
The thing I'd like to add here is a 404 error if .php extension is attempted. Here's what I have so far.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)\.html$ $1.php [NC,L]
RewriteRule ^$ /index.php [QSA,L]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.+)\.php - [F,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/favicon.ico
RewriteRule ^(.*)$ mycontroller.php [L,QSA]
</IfModule>
I tried adding what I gathered from this post, which worked, but it had the unfortunate side-effect of making my home page (without the /index.html written in the URL) to a 404. The author of the answer updated another line to prevent this from happening, but it doesn't seem compatible with my current script -- so my home page without the /index.html written in still goes to a 404 error.
It could be just that I don't have the order right, so any help would be appreciated. Many thanks in advance.
You can use rules like this:
DirectoryIndex index.php
RewriteEngine on
# block direct access to .php files
RewriteCond %{THE_REQUEST} \.php [NC]
RewriteRule \.php$ - [F,NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+?)\.html$ $1.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/favicon\.ico
RewriteRule ^ mycontroller.php [L]

Rewrite extension if file exists, redirect if file does not exist, with htaccess

Using .htaccess, I'd like to redirect files that do not exist to a controller page, and rewrite the extension of .php files that do exist to an .html extension. If a file exists and is an .html page, I'd like it to remain the same. Every time I try to inject the rewrite rule from .php to .html, I seem to mess up the redirect to the controller page. So I'm not sure where to go from here:
Options -Indexes
Options +FollowSymLinks
DirectoryIndex index.php
ErrorDocument 404 /404.php
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ mycontroller.php [L,QSA]
</IfModule>
Any help I'd be most grateful for.
Edit
I seem to have found most of the answer here (but I have to leave out the ReweriteBse or it doesn't work). The biggest issue is that now, my existing .html files don't work, it only serves my .php files with .html extensions and directs all else to the controller. Existing .html files go to my 404 page. I'd like to know how I can keep my existing .html files intact. My new code as follows:
RewriteEngine on
RewriteCond %{THE_REQUEST} (.*)\.php
RewriteRule ^(.*)\.php $1.html [R=301,L]
RewriteCond %{THE_REQUEST} (.*)\.html
RewriteRule ^(.*)\.html $1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ mycontroller.php [L,QSA]
Try:
<IfModule mod_rewrite.c>
RewriteEngine on
# If a request for a php file is made, check that it's actually a php file then redirect the browser
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*?)\.php($|\ )
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.*?)\.php$ /$1.html [L,R=301]
# If a request for an html file is made, check that it's a php file, and if so, serve the php file:
RewriteCond %{REQUEST_URI} ^/(.*?)\.html$
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^ /%1.php [L]
# Everything else goes to the controller
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ mycontroller.php [L,QSA]
</IfModule>
Try this (I have added a rewrite condition to avoid an infinite loop b yadding the parameter r=0 and testing if it exists) :
RewriteEngine on
RewriteCond %{QUERY_STRING} ^(.*&)?r=0(&.*)?$
RewriteRule ^(.*)\.php$ $1.html [L,R=301,QSA]
RewriteCond %{REQUEST_FILENAME} ^(.*)\.html$
RewriteCond %1.php -f
RewriteRule ^(.*)\.html$ $1.php?r=0 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ mycontroller.php [L,QSA]
I'm sure there are better ways to do this, but the following works for my needs. The other answers provided didn't seem to work despite my attempts.
Options -Indexes
Options +FollowSymLinks
DirectoryIndex index.php
ErrorDocument 404 /404.php
<IfModule mod_rewrite.c>
RewriteEngine on
# Check if .html file already exists -- if so, do nothing
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{THE_REQUEST} (.*)\.html
RewriteRule ^.*$ - [NC,L]
# Check if .php file already exists -- if so, rewrite extension to .html
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{THE_REQUEST} (.*)\.php
RewriteRule ^(.*)\.php $1.html [R=301,L]
RewriteCond %{THE_REQUEST} (.*)\.html
RewriteRule ^(.*)\.html $1.php [L]
# All else goes to the controller page
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ mycontroller.php [L,QSA]
</IfModule>

Categories