I have the following in my .htaccess file. Redirecting the www. works perfectly, as does the custom 404 message. The removal of .php from the file extension fails to work. domain.com/file returns the 404 message, with domain.com/file.php working fine.
RewriteEngine On
RewriteBase /
#
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
#
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^\.]+)/$ $1.php
#
ErrorDocument 404 /404.php
Also if it helps this is in my 000-default.conf file
<Directory /var/www>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
Write the code in .htaccess file in your project folder.it might be help for you
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
Related
I'm working with wampserver on windows 10 for local development.
after that i made my script live and it works fine but after some days i implemented url rewrite.
my scipts works fine on live server.
now i want to do some changes so i made it local for development and i'm getting Forbidden on my local host.
here is rewrite rules
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)/([-\w]+)/(\d+)/([-\w]+)$ $1.php?cat_name=$2&vid=$3&vbiz_name=$4 [NC,L]
RewriteRule ^(.*)/([-\w]+)/(\d+)$ $1.php?cat_name=$2&page=$3 [NC,L]
RewriteRule ^(.*)/([-\w]+)$ $1.php?cat_name=$2 [NC,L]
RewriteRule ^(.*)/$ $1.php [NC,L]
# Error Documents
ErrorDocument 404 /error/404.php
ErrorDocument 500 /error/500.php
RewriteCond %{REQUEST_URI} ^/404/$
RewriteRule ^(.*)$ 404.php [L]
RewriteCond %{REQUEST_URI} ^/500/$
RewriteRule ^(.*)$ 500.php [L]
</IfModule>
This is Virtual Host details i'm using.
<VirtualHost *:80>
ServerName devproject
DocumentRoot "g:/dev-project"
<Directory "g:/dev-project/">
Options +Indexes +Includes +FollowSymLinks +MultiViews
AllowOverride All
Require local
</Directory>
</VirtualHost>
index page works fine...
Please help me out..
Solved...
I just added a new line
Options FollowSymLinks
just after
RewriteEngine on
And it works on windows as well as on Linux both,
New Rules looks like
<IfModule mod_rewrite.c>
RewriteEngine on
Options FollowSymLinks
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)/([-\w]+)/(\d+)/([-\w]+)$ $1.php?cat_name=$2&vid=$3&vbiz_name=$4 [NC,L]
RewriteRule ^(.*)/([-\w]+)/(\d+)$ $1.php?cat_name=$2&page=$3 [NC,L]
RewriteRule ^(.*)/([-\w]+)$ $1.php?cat_name=$2 [NC,L]
RewriteRule ^(.*)/$ $1.php [NC,L]
# Error Documents
ErrorDocument 404 /error/404.php
ErrorDocument 500 /error/500.php
RewriteCond %{REQUEST_URI} ^/404/$
RewriteRule ^(.*)$ 404.php [L]
RewriteCond %{REQUEST_URI} ^/500/$
RewriteRule ^(.*)$ 500.php [L]
</IfModule>
I have a laravel application installed on a shared hosting package and I'm using https://github.com/JosephSilber/page-cache to cache pages.
The cached pages are stored in the following server folder /home/username/public_html/app_name/public/page-cache/article
The following code is in the .htaccess found in public folder
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Serve Cached Page If Available...
RewriteCond %{REQUEST_URI} ^/?$
RewriteCond %{DOCUMENT_ROOT}/public/page-cache/pc__index__pc.html -f
RewriteRule .? public/page-cache/pc__index__pc.html [L]
RewriteCond %{DOCUMENT_ROOT}/public/page-cache%{REQUEST_URI}.html -f
RewriteRule . public/page-cache%{REQUEST_URI}.html [L]
RewriteCond %{DOCUMENT_ROOT}/public/page-cache%{REQUEST_URI}.json -f
RewriteRule . public/page-cache%{REQUEST_URI}.json [L]
# Send Requests To Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
I have tried to debug the values of DOCUMENT_ROOT AND REQUEST_URI and I'm getting the following:
%{DOCUMENT_ROOT} = /home/username/public_html/app_name and %{REQUEST_URI} = /article/race-policing-and-the-universal-yearning-for-safety
With the above code in .htaccess i'm not managing to direct it to the cache pages but its going directly to the application even though the file exists.
e.g. /home/username/public_html/app_name/public/page-cache/article/race-policing-and-the-universal-yearning-for-safety.html
There is also the following .htaccess file in the app_name folder.
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_URI} (\.\w+$) [NC]
RewriteRule ^(.*)$ public/$1
#disable directory browsing
Options -Indexes
#PROTECT ENV FILE
<Files .env>
order allow,deny
Deny from all
</Files>
#PROTECT ENV FILE
<Files .htaccess>
order allow,deny
Deny from all
</Files>
I would appreciate if someone can help me identify the reason why the .htaccess is not loading the cache page when it exists. Stay safe guys :).
I don't understand htaccess at all except from it's function to "pretify" url's and I know this question has been asked a milion times, i have tried nearly all of them but none of the answers worked for me.
So basically i have givin my own go at creating a cms and a structure to go with it. the "cms" is located in a subfolder called system with it's own respectable index.php
my htaccess is configured to take the var "page" and retract the .php extension from it in the url. everything works fine until i start calling pages from the system subfolder.
My question is, how can i get htacces to ignore this folder and continue it's rewriting rule despite the user currently being located in the subfolder?
my htaccess is set up as follows:
RewriteEngine On
Options +FollowSymLinks
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\\.]+)$ $1.php [NC,L]
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.*)$ index.php?page=$1 [QSA]
<Files .htaccess>
order allow,deny
deny from all
</Files>
ErrorDocument 404 home
ErrorDocument 403 home
#php_flag display_errors off
Have it like this:
ErrorDocument 404 /home
ErrorDocument 403 /home
RewriteEngine On
Options +FollowSymLinks
# skip subfolder from any rules
RewriteRule ^subfolder(/.*)?$ - [L,NC]
# add .php extension
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^system/(.*)$ system/index.php?page=$1 [L,NC,QSA]
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.*)$ index.php?page=$1 [L,QSA]
<Files .htaccess>
order allow,deny
deny from all
</Files>
I'm trying to rewrite example.com/news.php to example.com/news or example.com/news/. With my code example.com/news works just fine but example.com/news/ gives me a 404 Not Found.
My code:
<Directory /var/www/>
Options -Indexes SymLinksIfOwnerMatch -MultiViews
AllowOverride None
Order allow,deny
allow from all
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^games/csgo/([0-9]+)$ /games/csgo/view.php?id=$1 [L]
</IfModule>
</Directory>
How can I redirect example.com/news.php to example.com/news AND example.com/news/?
You should better stick to one url type! (with or without trailing slash) and redirect the wrong type to the other,
this code is for removing slash from the end of url :
RewriteRule ^(.*)/$ /$1 [L,R=301]
If you add RewriteCond %{REQUEST_FILENAME} !-d and RewriteCond %{REQUEST_FILENAME}.php -f before that it works also with sub directories.
I have created a website on my localhost,
Inside htdocs I have added a folder 'sitename' this contains all files including .htaccess. This is working fine on my local, but when I am uploading it on server the its not working.
.htaccess code
Options +FollowSymlinks
RewriteEngine on
RewriteBase /sitename/
# redirect to .php-less link if requested directly
#RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
#RewriteRule ^ /sitename/%1 [R,L,NC]
ErrorDocument 404 /sitename/404
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/sitename/$1.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
What should be the replacement for 'sitename' while uploading on cpanel
public_html??
If you keep your scripts in a sub-folder (example: /public_html/sitename/ than below should work for you). However, if you keep your scripts directly in /public_html/ (example: /public_html/index.php) than replace RewriteBase /sitename/ with RewriteBase /
Options +FollowSymlinks
<IfModule mod_rewrite.c>
# let the PHP knows that the mod_rewrite module is ENABLED.
SetEnv HTTP_MOD_REWRITE On
RewriteEngine on
RewriteBase /sitename/
</IfModule>
#prevent direct access to certain file types
<FilesMatch "\.(htaccess|htpasswd|ini|log|sh|inc|bak|psd|DS_Store|project)$">
Order Allow,Deny
Deny from all
</FilesMatch>
ErrorDocument 404 /sitename/404.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/sitename/$1.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
Let us know if you get any error message - it would help.