My htaccess files contains only a few lines that firstly remove the www and then add ".php" to the slug to get the correct php file, so
www.kalicup.fr/seo
should rewrite to
kalicup.fr/seo
and then display the file seo.php (without the .php extension displaying in the url itself)
at the moment
kalicup.fr/seo
correctly displays seo.php without showing the file extension.
however, when I try
www.kalicup.fr/seo
it rewrites to
kalicup.fr/seo.php
adding the .php extension in the url
so there's abviously a problem in my htaccess but I can't see it !
here's my code
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
ErrorDocument 404 /404.php
# redirect the url with www to url without
RewriteCond %{HTTP_HOST} ^www\.(([a-z0-9_]+\.)?kalicup\.fr)$ [NC]
RewriteRule .? http://%1%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.(([a-z0-9_]+\.)?kalicup\.co\.uk)$ [NC]
RewriteRule .? http://%1%{REQUEST_URI} [R=301,L]
# add .php to urls
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
can anyone see the problem ?
Use that in your .htaccess:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
ErrorDocument 404 /404.php
# redirect the url with www to url without
RewriteCond %{HTTP_HOST} ^www\.(([a-z0-9_]+\.)?kalicup\.(?:fr|co\.uk))$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
# add .php to urls
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
Only one test for .fr and .co.uk.
And -MultiViews: http://httpd.apache.org/docs/2.2/en/mod/core.html#options
The effect of MultiViews is as follows: if the server receives a request for /some/dir/foo, if /some/dir has MultiViews enabled, and /some/dir/foo does not exist, then the server reads the directory looking for files named foo.*, and effectively fakes up a type map which names all those files, assigning them the same media types and content-encodings it would have if the client had asked for one of them by name. It then chooses the best match to the client's requirements.
http://httpd.apache.org/docs/2.2/en/content-negotiation.html
Related
I used to run a Wordpress website, but started building my own in php. I was building it on a subdirectory on my server and now want to launch the new website. The structure is as follows:
public_html/newversion/index.php
public_html/newversion/category.php
public_html/newversion/category/information.php
I've edited the .htaccess file in public_html to force non-www and https, set newversion as the new document root, and some code to remove the .php extension from URLs (see the .htaccess code below).
That means that I want newversion/index.php to show up when someone goes to example.com, instead of the person needing to go to example.com/newversion/index.php.
Everything works fine, except when I try to access category.php or information.php. Then the browser is automatically redirected to example.com/newversion/category.php and I get a 404 error. When I try to access information.php, the browser tries to open example.com/category/information.php, but I also get a 404 error.
What am I doing wrong?
(FYI, Wordpress is not installed anymore)
# Apache Rewrite Rules
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
# Base Redirects #
# Remove trailing slash from non-filepath urls
RewriteCond %{REQUEST_URI} /(.+)/$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ https://example.com/%1 [R=301,L]
# Include trailing slash on directory
RewriteCond %{REQUEST_URI} !(.+)/$
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+)$ https://example.com/$1/ [R=301,L]
# Force HTTPS and remove WWW
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [OR,NC]
RewriteCond %{https} off
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
# Change root folder to subfolder of public_html
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/newversion%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/newversion%{REQUEST_URI} -d [OR]
RewriteCond %{DOCUMENT_ROOT}/newversion%{REQUEST_URI} -l
RewriteRule ^ /newversion%{REQUEST_URI} [L]
RewriteEngine On
# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://%{HTTP_HOST}/$1 [R=301,L]
# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [L]
# Set php version
AddHandler application/x-httpd-recommended-php .php .php5 .php4 .php3
</IfModule>
# Restrict access
<Files .htaccess>
order allow,deny
deny from all
</Files>
Took me a long time figuring this out, but I now know what people mean with .htaccess Rewrite voodoo... So, for anyone still needing an answer:
In the end, the order in which you declare htaccess rules and the subdirectory in which you do it matters a lot. Don't ask me how it works exactly, because I don't fully understand. However, this code worked for me.
The underlying code achieves the following:
website root in subdirectory
force non-www and https
remove .php extension from url
open subdirectories as example.com/foo/ whether or not entered with trailing slash
open files in subdirectories as example.com/foo/bar whether or not entered with a trailing slash
.htaccess file in public_html:
# htaccess for public_html
Options -Indexes +FollowSymlinks -MultiViews
ErrorDocument 404 /404.php
# Remove www and force https
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
# add a trailing slash if public/$1 is a directory
RewriteCond %{DOCUMENT_ROOT}/subdirectory/$1 -d
RewriteRule ^(.*?[^/])$ %{REQUEST_URI}/ [L,R=301]
#Prevent direct access to PHP Scripts
RewriteCond %{THE_REQUEST} \.php[?/\s] [NC]
RewriteRule ^ - [R=404,L]
RewriteRule ^/?$ subdirectory/ [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^((?!subdirectory/).*)$ subdirectory/$1 [L,NC]
Then in the subdirectory add the following htaccess file:
# htaccess for subdirectory
# Remove trailing slashes (keep as first rule!)
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ /$1 [NE,R=301,L]
# Open page without php extension
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L]
I'm trying to remove the ".php" extension from the URLs of a site using this code (which I admit I copy/pasted from other questions here) in the .htaccess file:
RewriteEngine on
Options +FollowSymLinks
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
Other lines of the .htaccess file do work, for instance, I have an error redirect and:
RewriteCond %{HTTP_HOST} ^www\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [R=301,L]
So, I know the .htaccess file is in service in general.
I don't know what could go wrong in this area, so I'm not sure where to begin troubleshooting. Does anyone have pointers?
Thanks in advance.
Given that your domain account is /home/youraccount/public_html, your .htaccess would be inside the public_html folder with the following content:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /
# First we redirect the www to non-www
RewriteCond %{HTTP_HOST} ^www\.domain\.com$ [NC]
RewriteRule ^/?(.*)$ http://domain.com/$1 [R=301,L]
# now we redirect phpless URLs internally to the php
# if folder does not exist
RewriteCond %{REQUEST_FILENAME} !-d
# but the file exists and ends with .php
RewriteCond %{REQUEST_FILENAME}\.php -f
# redirect to name.php
RewriteRule ^([^/]+)/?$ $1.php [L]
NOTE: If you have more rules this may conflict so I would have to look at the rest of your rule but basically the above should work as expected.
You will be able to access both:
domain.com/index
and
domain.com/index/
And it would redirect to your file index.php.
I am using the following code in .htaccess file
DirectoryIndex home.php
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^$ - [L]
RewriteRule ^/?([^\./]*)[:;,\.]*$ $1.php
RewriteRule ^([a-zA-Z0-9_,-]+)/([0-9]*)$ listing.php?business=$2
This code hides the .php extension of the PHP files in the root folder only, But I want to apply it to the PHP files in all subfolders.
I also want to deny access using the original URL , that is the URL includes the .php extension.
I also want the third rule should be worked.
Replace your code with this:
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^([a-zA-Z0-9_,-]+)/([0-9]+)$ listing.php?business=$2 [L,QSA]
## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php[\s\?] [NC]
RewriteRule ^ - [F]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ /$1.php [L]
I'm having an issue with .htaccess where I have managed to successfully rewrite the URLs but the content is no longer loading.
The following is my htaccess file.
I'm aiming for all my .html pages (the site is made of .html static pages) to have their extensions removed. However, I require the .html URLs to 301 redirect to the new URL's so that my SEO does not take a hit from these changes.
Example:
Original: www.example.co.uk/page.html
Desired: www.example.co.uk/page/
It is important that the original URL redirects the new URL's though.
Options +FollowSymLinks
RewriteEngine on
# REDIRECT yourdomain.com TO www.yourdomain.com
RewriteCond %{HTTP_HOST} !^www.example.co.uk$ [NC]
RewriteRule ^(.*)$ http://www.example.co.uk/$1 [L,R=301]
RewriteRule (.+)\.html?$ http://www.example.co.uk/$1/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(([^/]*/)*[^/.]+)$ $1.html [L]
I have tried the htaccess above and I've also tried the variation below, but neither has worked as desired. Any help would be much appreciated.
Options +FollowSymLinks
RewriteEngine on
# REDIRECT yourdomain.com TO www.yourdomain.com
RewriteCond %{HTTP_HOST} !^www.example.co.uk$ [NC]
RewriteRule ^(.*)$ http://www.example.co.uk/$1 [L,R=301]
RewriteRule (.+)\.html?$ http://www.example.co.uk/$1/ [R=301,L]
You need to change 2 things.
Add this right after your redirect to prevent a redirect loop:
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
change the last rule to this:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/(.*?)/?$
RewriteCond %{DOCUMENT_ROOT}/%1\.html -f
RewriteRule ^ /%1.html [L]
I have an already ok htaccess script that is designed to forward /about to /about.php without displaying it, but I was wondering if anyone knew how to modify my .htaccess file to to forward requests to /about.html to /about (using 302 rather than a hidden redirect)
CheckSpelling on
Options -Indexes
Options +FollowSymlinks
DirectoryIndex index.php
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ /index.php?page=$1 [L,QSA]
try
RewriteCond %{REQUEST_URI} ^/(.+)\.html$ [NC]
RewriteRule . /%1 [R=302,L]