I want to allow access to directories, while still maintaining the current configuration.
The Current configuration is this:
DirectoryIndex index.php Index.php
Options +FollowSymLinks -MultiViews
#Turn mow_rewrite on
RewriteEngine On
RewriteBase /
## hiding .php extensions, and externally redirecting /dir/file.php to dir/file
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
##internally redirecting /dir/file.php to dir/file
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
The thing is, this htaccess current configuration removes .php extension from files,
but i think, it's affecting directories, and restricting access to it.
Please what do i do to solve this
Try this code:
DirectoryIndex index.php Index.php
Options +FollowSymLinks -MultiViews
#Turn mow_rewrite on
RewriteEngine On
RewriteBase /
## hiding .php extensions, and externally redirecting /dir/file.php to dir/file
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NE]
##internally redirecting /dir/file.php to dir/file
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]
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 am trying to configure and write my .htaccess file correctly. There are some things I already tried, but somehow they dont all work out.
I am trying to accomplish the following redirects:
Redirect from HTTP to HTTPS for all folders, including subfolders
Remove ".html" endings so the url shows www.domain.com/photos instead of www.domain.com/photos.html
Remove "index.html" when on home page, so it only shows www.domain.com
Some things work, but when going in a subdirectory like www.domain.com/sub, the page changes from HTTPS to HTTP.
Also, when accessing the page via HTTPS, it doesnt correctly load the font, that is specified in the HTML file
()
See my code below:
<IfModule mod_rewrite.c>
AddType text/x-component .htc
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} !=on
# This checks to make sure the connection is not already HTTPS RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
# This rule will redirect users from their original location, to the same location but using HTTPS. # i.e. http://www.example.com/foo/ to https://www.example.com/foo/ # The leading slash is made optional so that this will work either in httpd.conf or .htaccess context
# remove .html; use THE_REQUEST to prevent infinite loops
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.html\ HTTP
RewriteRule (.*)\.html$ $1 [R=301]
# remove index
RewriteRule (.*)/index$ $1/ [R=301]
# remove slash if not directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/ $1 [R=301]
# add .html to access file, but don't redirect
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.html [L]
</IfModule>
I figured it out and it works so far:
<IfModule mod_rewrite.c>
AddType text/x-component .htc
RewriteEngine On
RewriteBase /
# Redirect to www
RewriteCond %{HTTP_HOST} !^www\.domain\.ch [NC,OR]
# Redirect to https
RewriteCond %{HTTP:CF-Visitor} '"scheme":"http"'
RewriteRule ^ https://www.domain.ch%{REQUEST_URI} [NE,R=301,L]
# remove .html; use THE_REQUEST to prevent infinite loops
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.html\ HTTP
RewriteRule (.*)\.html$ $1 [R=301]
# remove index
RewriteRule (.*)/index$ $1/ [R=301]
# remove slash if not directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/ $1 [R=301]
# add .html to access file, but don't redirect
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.html [L]
</IfModule>
I've been trying to find a solution to my situation but none worked. I have the following htaccess file which does the following:
It redirects site.com/m/page to site.com/m/#page
I redirects site.com/d/page to site.com/m/#page
It deletes .php extension and the common_ pattern which I had in front of the pages
The code so far:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^[dm]/(.+)$ /m/#$1 [R=302,NE,L,NC]
## hide .php extension
RewriteCond %{THE_REQUEST} \s/+common_([^.]+)\.php[\s?] [NC]
RewriteRule ^ %1 [R=302,L,NE]
## To internally forward
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/common_$1.php -f
RewriteRule ^(.+?)/?$ common_$1.php [L]
I would like the folowing:
a) If some user enters from an iPhone 'site.com/m/...' , continue (do nothing)
b) If some user enters from a desktop ar iPad 'site.com/m/page' to redirect him to 'site.com/d/#page'
I would really appreciate some help.
You can use a new rule for this:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_USER_AGENT} !iphone [NC]
RewriteRule ^m/(.+)$ /d/#$1 [R=302,NE,L,NC]
RewriteRule ^[dm]/(.+)$ /m/#$1 [R=302,NE,L,NC]
## hide .php extension
RewriteCond %{THE_REQUEST} \s/+common_([^.]+)\.php[\s?] [NC]
RewriteRule ^ %1 [R=302,L,NE]
## To internally forward
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/common_$1.php -f
RewriteRule ^(.+?)/?$ common_$1.php [L]
Why each page redirects to domain? I move dkcherta.ru -> dkchertanovo.ru and want for example http://dkcherta.ru/index.php?id=80 -> http://dkchertanovo.ru/index.php?id=80
My .htaccess:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?dkcherta\.ru$ [NC]
RewriteRule ^ http://dkchertanovo.ru/%{REQUEST_URI} [L,R=301]
RewriteRule ^rss_afisha[/]?$ /index.php?id=230 [L]
RewriteRule ^rss_news[/]?$ /index.php?id=231 [L]
ErrorDocument 404 http://dkchertanovo.ru/
# For full documentation and other suggested options, please see
# http://svn.modxcms.com/docs/display/MODx096/Friendly+URL+Solutions
# including for unexpected logouts in multi-server/cloud environments
RewriteBase /
# Сливка доменного имени
#RewriteCond %{HTTP_HOST} ^dkchertanovo\.ru$ [NC]
#RewriteRule ^(.*)$ http://www.dkchertanovo.ru/$1 [R=301,L]
RemoveHandler .php .htm .html
AddType application/x-httpd-php .php .htm .html
AddHandler application/x-httpd-php .php .htm .html
php_flag register_globals off
# Fix Apache internal dummy connections from breaking [(site_url)] cache
RewriteCond %{HTTP_USER_AGENT} ^.*internal\ dummy\ connection.*$ [NC]
RewriteRule .* - [F,L]
# Exclude /assets and /manager directories from rewrite rules
RewriteRule ^(manager|assets) - [L]
# For Friendly URLs
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
### force www
#RewriteCond %{HTTP_HOST} ^www.(.+)$ [NC]
#RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
#RewriteBase /
# Reduce server overhead by enabling output compression if supported.
#php_flag zlib.output_compression On
#php_value zlib.output_compression_level 5
You don't need multiple lines of these:
Options +FollowSymLinks -MultiViews
RewriteEngine On
Just put only once at the top and it should be effective for rest of the file.
Then to solve your problem move this rule on top of all other rules in your .htaccess file:
RewriteCond %{HTTP_HOST} ^(www\.)?dkcherta\.ru$ [NC]
RewriteRule ^ http://dkchertanovo.ru%{REQUEST_URI} [L,R=301]
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]