htaccess - change file dir with filter - .htaccess

I'm currently trying to make a htaccess thing to find the file in new/ if the request uri not contains api or list. I tried to do a thing like this but, it shows 403 error. Are there a easier way to filter, or?
RewriteEngine on
RewriteCond %{REQUEST_URI} /api/ [NC]
RewriteCond %{REQUEST_URI} /list/ [NC]
RewriteRule ^/(.*)$/ new/$1 [L, R]
Full htaccess code
#remove html file extension
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html [NC,L]
#remove php file extension
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
#set max post size
php_value memory_limit 2048M
php_value max_execution_time 3000
php_value max_input_time 6000
php_value post_max_size 800M
php_value upload_max_filesize 200M
#Caching schema
<FilesMatch "\.(png|svg|css|eot|ttf|woff|woff2|otf|js|css)$">
Header set Cache-Control "public, max-age=86400000"
</FilesMatch>
DirectoryIndex index.html
#Custom 404 errors
ErrorDocument 404 /404.html
#Prevent viewing of .htaccess file
<Files .htaccess>
order allow,deny
deny from all
</Files>
<Files error_log>
order allow,deny
deny from all
</Files>
#Prevent directory listings
Options All -Indexes

EDIT: With OP's full htaccess file please try following.
#remove html file extension
RewriteEngine on
##For external redirect rule.
RewriteCond %{THE_REQUEST} !\s/(?:api|list).*\s [NC]
RewriteRule ^(.*)/?$ new/$1 [L,R=301]
##For internal redirect rule.
RewriteRule ^new/(.*)/?$ $1 [L,NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)/?$ $1.html [NC,L]
#remove php file extension
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)/?$ $1.php [QSA,NC,L]
#set max post size
php_value memory_limit 2048M
php_value max_execution_time 3000
php_value max_input_time 6000
php_value post_max_size 800M
php_value upload_max_filesize 200M
#Caching schema
<FilesMatch "\.(png|svg|css|eot|ttf|woff|woff2|otf|js|css)$">
Header set Cache-Control "public, max-age=86400000"
</FilesMatch>
DirectoryIndex index.html
#Custom 404 errors
ErrorDocument 404 /404.html
#Prevent viewing of .htaccess file
<Files .htaccess>
order allow,deny
deny from all
</Files>
<Files error_log>
order allow,deny
deny from all
</Files>
#Prevent directory listings
Options All -Indexes
With your shown attempts/rules, could you please try following once. Please make sure to clear browser cache before testing your URLs.
RewriteEngine on
##For external redirect rule.
RewriteCond %{REQUEST_URI} !^/(?:api|list)/? [NC]
RewriteRule ^(.*)/?$ new/$1 [L,R=301]
##For internal redirect rule.
RewriteRule ^new/(.*)/?$ $1 [L,NC]
OR try following rules set: Please make sure try only 1 set at a time, either above OR following.
RewriteEngine on
##For external redirect rule.
RewriteCond %{THE_REQUEST} !\s/(?:api|list).*\s [NC]
RewriteRule ^(.*)/?$ new/$1 [L,R=301]
##For internal redirect rule.
RewriteRule ^new/(.*)/?$ $1 [L,NC]

You may try these rules in your site root .htaccess:
RewriteEngine On
# redirect to /new if not /api, /list, /new and file exists in /new
RewriteCond %{THE_REQUEST} !\s/+(new|api|list)/ [NC]
RewriteCond %{DOCUMENT_ROOT}/new/$0 -f
RewriteRule .* /new/$0 [L,R=301,NE]
## remaining rules go below this line ##
# remove html file extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*?)/?$ $1.html [L]
# remove php file extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
Make sure to test it after clearing your browser cache or test from a new browser.

Related

direct access denied on subdomain's subdirectory

my directory:
domain.com/
-----------subdomain/
---------------------subdirectory/
I've created subdomain as subdomain.domain.com
I need to access subdomain.domain.com/subdirectory but is shows: 404
I can access domain.com/subdomain/subdirectory/
please tell me how to do that.
### EDITED ###########
My .htaccess
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [QSA]
RewriteRule ^([0-9]+^)$ download/content/$1 [L]
<IfModule mod_php5.c>
php_value upload_max_filesize 200M
php_value post_max_size 200M
</IfModule>
if I add the following before my rule, it stops my existing. and if I add it after my rule. it doesn't execute because of [L] flag
RewriteCond %{HTTP_HOST} ^wap\.
RewriteRule !^wap/ wap%{REQUEST_URI} [L]
Have this rule in your root htaccess:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^wap\.
RewriteRule !^wap/ wap%{REQUEST_URI} [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
RewriteRule ^([0-9]+)$ download/content/$1 [L]
<IfModule mod_php5.c>
php_value upload_max_filesize 200M
php_value post_max_size 200M
</IfModule>

Rewriting urls with Htaccess like filename/param1/param2 etc etc

I'm looking for a solution for the following situation. I'd like to covert the following url
www.example.com/filename.php?p1=1&p2=2 etc
to
www.exmaple.com/filename/1/2 etc
I managed to do this, with index.php, but I'd like to use other file aswell, not only the index. Is it possible? Here's my current code.
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
# add .php to access file, but don't redirect
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
RewriteRule ^(filename)/([^/]+)/([^/]+)$ /$1.php?p1=$2&p2=$3 [L,QSA,NC]
ErrorDocument 404 /404.php
# display no errs to user
php_flag display_startup_errors off
php_flag display_errors off
php_flag html_errors off
This works for the php removal. But if I add parameters after it, it gives me error 500
Insert this rule just below RewriteEngine On line:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(filename)/([^/]+)/([^/]+)/?$ /$1.php?p1=$2&p2=$3 [L,QSA,NC]

configuring .htaccess file after migration in expressionengine

So basically I've moved a site from the original top domain to a folder in my site but the page is blank due to the .htaccess not automatically redirect to /en/
I tried placed my domain, julianjosephs.com before the existing domain but it didn't work. Not sure what to do now.
Original: http://morganstachs.com/en/
Development: http://julianjosephs.com/morganstachs.com/
.htaccess file contents
# -- LG .htaccess Generator Start --
# .htaccess generated by LG .htaccess Generator v1.0.0
# http://leevigraham.com/cms-customisation/expressionengine/addon/lg-htaccess-generator/
# secure .htaccess file
<Files .htaccess>
order allow,deny
deny from all
</Files>
# Dont list files in index pages
IndexIgnore *
# EE 404 page for missing pages
ErrorDocument 404 /index.php?/
# Simple 404 for missing files
<FilesMatch "(\.jpe?g|gif|png|bmp)$">
ErrorDocument 404 "File Not Found"
</FilesMatch>
RewriteEngine On
RewriteBase /
# remove the www
RewriteCond %{HTTP_HOST} ^(www\.$) [NC]
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Add a trailing slash to paths without an extension
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule ^(.*)$ $1/ [L,R=301]
# Remove index.php
# Uses the "include method"
# http://expressionengine.com/wiki/Remove_index.php_From_URLs/#Include_List_Method
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5})$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} ^/(_includes|site|landing_pages|listings_shortcut|offers|scripts|xml|en|sp|fr|members|P[0-9]{2,8}) [NC]
RewriteRule ^(.*)$ /index.php?/$1 [L]
# Remove IE image toolbar
<FilesMatch "\.(html|htm|php)$">
Header set imagetoolbar "no"
</FilesMatch>
# -- LG .htaccess Generator End --
RewriteCond %{HTTP_HOST} ^admin\.morganstachs\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.admin\.morganstachs\.com$
RewriteRule ^/?$ "http\:\/\/morganstachs\.com\:2082" [R=301,L]

remove www from https

I'm trying to find a htaccess rewrite to remove www. from https: pages only. Every article I've found removes www for all pages.
What do I need in .htaccess to do this?
https://www.mydomain.com should be https://mydomain.com
https://mydomain.com should remain https://mydomain.com
http://www.mydomain.com should remain http://www.mydomain.com
http://mydomain.com should remain http://mydomain.com
I've been able to get the following working:
RewriteCond %{HTTPS}s ^on(s)|
RewriteCond http%1://%{HTTP_HOST}%{REQUEST_URI} ^(https?://)www\.(.+) [NC]
RewriteRule ^ %1%2 [L,R,QSA]
But this rewrites both https and http
Here is my current .htaccess
SetEnv DEFAULT_PHP_VERSION 5
RewriteEngine on
RewriteOptions MaxRedirects=1
# You may need to uncomment the following line on some hosting environments,
# for example on unitedhosting.co.uk
# RewriteBase /
# The following line has been added in order to exclude the webim
# directory from the LemonStand URL processing.
RewriteCond %{REQUEST_URI} !^/webim
#
# Do not allow executing any PHP scripts
#
RewriteRule ^(.*).php$ index.php [L]
#
# The following section automatically adds a trailing slash to all URLs
#
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteCond %{HTTP:X-REQUESTED-WITH} !^(XMLHttpRequest)$
RewriteCond %{REQUEST_METHOD} !^HEAD$
RewriteCond %{REQUEST_METHOD} !^POST$
RewriteRule (.*)([^/])$ %{REQUEST_URI}/ [R=301,L]
# The following line has been added in order to exclude the webim
# directory from the LemonStand URL processing.
RewriteCond %{REQUEST_URI} !^/webim
#
# Product files downloading URL
#
RewriteRule (^download_product_file/.*) index.php?q=/$1 [L,QSA]
# The following line has been added in order to exclude the webim
# directory from the LemonStand URL processing.
RewriteCond %{REQUEST_URI} !^/webim
#
# Administration Area file downloading URL
#
RewriteRule ls_backend/files/get/(.*) index.php?q=/backend_file_get/$1 [L]
# The following line has been added in order to exclude the webim
# directory from the LemonStand URL processing.
RewriteCond %{REQUEST_URI} !^/webim
#
# All other requests
#
RewriteCond %{REQUEST_URI} !(\.(ico|js|jpg|gif|css|png|swf|flv|txt|xml|xls|pdf|eot|woff|ttf|svg|mp4)$)
RewriteCond %{REQUEST_URI} !(phproad/thirdpart/.*)
RewriteRule ^(.*)$ index.php?q=/$1 [L,QSA]
ErrorDocument 404 "File not found"
#
# PHP configuration
#
<IfModule mod_php5.c>
php_flag session.auto_start off
php_value session.cookie_lifetime 31536000
php_flag session.use_cookies on
php_flag session.use_only_cookies on
php_value session.name FWCSESSID
php_flag short_open_tag on
php_flag asp_tags on
php_flag magic_quotes_gpc off
php_value date.timezone GMT
php_value post_max_size 100M
php_value upload_max_filesize 100M
php_value memory_limit 264M
</IfModule>
RewriteEngine On
# Check that you're on port 443 and the hostname starts with www
RewriteCond %{SERVER_PORT} ^443
RewriteCond %{HTTP_HOST} ^www\.
# Redirect to domain without the www
RewriteRule (.*) https://example.com$1 [L,R,QSA]
This should work:
RewriteEngine On
RewriteCond %{HTTPS} =on
RewriteCond %{HTTP_HOST} ^www\.
RewriteRule ^(.*)$ https://mydomain.com/$1 [R,QSA,L]
If HTTPS is on and HTTP_HOST is www.mydomain.com, then it redirects to https://mydomain.com/.
We can achieve this via apache virtualhost
<VirtualHost *:80>
ServerName www.example.com
Redirect / https://example.com/
</VirtualHost>
<virtualHost *:443>
DocumentRoot /var/www/html/example.com
ServerName example.com
</VirtualHost>
Add one more condition (to your existing redirect rule):
RewriteCond %{HTTPS} ^on$
This will remove www. from both https and http, and without having to add your domain.
# remove www. (generic method with HTTPS support)
RewriteCond %{HTTP_HOST} ^www\.
RewriteCond %{HTTPS}s ^on(s)|off
RewriteCond http%1://%{HTTP_HOST} ^(https?://)(www\.)?(.+)$
RewriteRule ^ %1%3%{REQUEST_URI} [R,L]
Meu arquivo final .htaccess sendo usado dentro de uma pasta e com redirecionamento de https também.
My finishing file .htaccess inner using path url (ex: example.com/intranet):
Options -Indexes
Options +FollowSymLinks
Options -MultiViews
DefaultLanguage pt-BR
ServerSignature Off
DirectoryIndex index.php
RewriteEngine On
# Check that you're on port 443 and the hostname starts with www
RewriteCond %{SERVER_PORT} ^443
RewriteCond %{HTTP_HOST} ^www\.
# Redirect to domain without the www
RewriteRule (.*) https://example.com/intranet/$1 [L,R,QSA]
RewriteCond %{HTTPS} !=on
RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [L,R]

Help with URL rewriting using .htaccess

I have html static files on my server. I want the access rules as -
If user visits http://example.com/test/ he should get the contents of the file http://example.com/test.html
If the user visits http://example.com/test (without the trailing slash), he gets redirected to http://example.com/test/ (which runs rule 1 and gets him the contents of the file test.html)
Rules 1 and 2 should only fire if the file test.html exists.
So far, I have -
Options All -Indexes -Multiviews
# Do not return details of server
ServerSignature Off
<IfModule mod_rewrite.c>
DirectorySlash Off
RewriteEngine On
RewriteBase /
# If it's a request to index.html
RewriteCond %{THE_REQUEST} \ /(.+/)?index\.html(\?.*)?\ [NC]
# Remove it.
RewriteRule ^(.+/)?index\.html$ /%1 [R=301,L]
# Add missing trailing slashes to directories if a matching .html does not exist.
RewriteCond %{SCRIPT_FILENAME}/ -d
RewriteCond %{SCRIPT_FILENAME}.html !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]
# If it's a request from a browser, not an internal request by Apache/mod_rewrite.
RewriteCond %{ENV:REDIRECT_STATUS} ^$
# And the request has a HTML extension. Redirect to remove it.
RewriteRule ^(.+)\.html$ /$1 [R=301,L]
# If the request exists with a .html extension.
RewriteCond %{SCRIPT_FILENAME}.html -f
RewriteRule ^(.*)/?$ $1.html [QSA,L]
</IfModule>
Although it works for http://example.com/test, it fails for http://example.com/test/ (500 internal error)
Shouldn't the second last line take care of trailing slashes?
[Update]
If I use Gumbo suggestions (.htaccess follows), I get a 404 for both http://example.com/test and http://example.com/test/ (with trailing slash)
Options All -Indexes -Multiviews
# Do not return details of server
ServerSignature Off
ErrorDocument 404 /404.html
# Hide .htaccess
<Files ~ "^\.ht">
Order allow,deny
Deny from all
</Files>
<IfModule mod_rewrite.c>
DirectorySlash On
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^[^/]+$ %{REQUEST_URI}/ [L,R=301]
RewriteRule ^([^/]+)/$ $1.html [L]
</IfModule>
[Update 2]
I have given up. The code below works, but instead of forcing everything end with a / (http://example.com/test/), it removes the trailing slash (http://example.com/test). On the bright side, it ensures that the content is pointed to by only one url, preserving SEO. I'm going to live with it for now.
Options All -Indexes -Multiviews
# Do not return details of server
ServerSignature Off
ErrorDocument 404 /404.html
# Hide .htaccess
<Files ~ "^\.ht">
Order allow,deny
Deny from all
</Files>
<IfModule mod_rewrite.c>
DirectorySlash On
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ /$1 [R=301,L]
# If the request exists with a .html extension.
RewriteCond %{SCRIPT_FILENAME}.html -f
# And there is no trailing slash, rewrite to add the .html extesion.
RewriteRule [^/]$ %{REQUEST_URI}.html [QSA,L]
</IfModule>
Try these rules:
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^[^/]+$ %{REQUEST_URI}/ [L,R=301]
RewriteRule ^([^/]+)/$ $1.html [L]

Resources