Can anyone halp me with this .htaccess file, i've been trying to make an exception so i can access my subdomain which is
form.domain.com
i tried with
RewriteCond %{REQUEST_URI} !^/form/?$
and several other commands but with no luck
.htaccess :
#######################
# N - T H I N G ! #
#######################
# Apache options
Options +FollowSymLinks -Indexes
RewriteEngine on
# Allow only GET and POST verbs
RewriteCond %{REQUEST_METHOD} !^(GET|POST)$ [NC,OR]
# Ban Typical Vulnerability Scanners and others
# Kick out Script Kiddies
RewriteCond %{HTTP_USER_AGENT} ^(java|curl|wget).* [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^.*(libwww-perl|curl|wget|python|nikto|wkito|pikto|scan|acunetix).* [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^.*(winhttp|HTTrack|clshttp|archiver|loader|email|harvest|extract|grab|miner).* [NC,OR]
# Error Page
ErrorDocument 404 /404
# Redirect attachments
RewriteRule ^files/attachments/(.*)/(.*)/(.*)$ files/attachments/$1/$2/$3 [L]
# Redirect all requests to index.php
RewriteRule ^(.*)$ index.php [L,QSA]
To match a domain you should use %{HTTP_HOST} as in:
RewriteCond %{HTTP_HOST} ^form.domain.com$
RewriteRule ^.*$ - [L]
That says if the domain matches form.domain.com, allow the URL and stop processing rules.
See the Apache documentation for more details.
Related
I'm trying to create a simple htaccess script which redirects users to the appropriate page. So, as an example, if the file/folder doesn't exist, a user navigating to /listing/id will be shown listing.php?id=id instead, or if they navigate to /username they will be shown profile.php?id=username.
The problem is, when accessing /listing/id, I retrieve an internal server error. But not because of the RewriteRule that handles this part - but because of the .php extension removal part of my htaccess script (I figured this out when I commented it out). I have no idea why this isn't working, as I believe I have the correct flags set, and am using the right block of code.
My Code
Options +FollowSymLinks -Indexes -MultiViews
RewriteEngine On
# no php extension
# (/listing/id will work if this is commented out, but the other pages won't without the .php extension)
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
# redirect to www.*
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com [R=301,L]
# only allow rewriting to paths that don't exist
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.+) - [PT,L]
# external profile.php?id=$id to /$id
RewriteCond %{THE_REQUEST} /profile\.php\?id=([\w-]+) [NC]
RewriteRule ^ /%1? [R=301,L]
# /listing/$id
RewriteRule ^listing/([\w-]+)/?$ listing.php?id=$1 [L,QSA,NC]
# /$username
RewriteRule ^([\w-]+)/?$ profile.php?id=$1 [L,QSA]
Example cases
/ (index) works
/search works (the file exists so it redirects to search page not a user named search)
/username works (username.php doesn't exist so it redirects to the profile page)
/listing/test returns Internal Server Error 500 (removing the no PHP extension part of my htacess causes this page to work normally, but now all the others will require .php to be appended)
All help is appreciated, Cheers.
Try your rules in this order:
Options +FollowSymLinks -Indexes -MultiViews
RewriteEngine On
# redirect to www.*
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
# external profile.php?id=$id to /$id
RewriteCond %{THE_REQUEST} /profile\.php\?id=([\w-]+)\s [NC]
RewriteRule ^ /%1? [R=301,L]
# only allow rewriting to paths that don't exist
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
# /listing/$id
RewriteRule ^listing/([\w-]+)/?$ listing.php?id=$1 [L,QSA,NC]
# no php extension
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
# /$username
RewriteRule ^([\w-]+)/?$ profile.php?id=$1 [L,QSA]
I'm trying to hide a subdirectory from the URL with .htaccess. I've got a PHP script which is executed everytime a client enters the website. It's index.php in root Apache directory. This script determines the language to use in the website and redirects to the target directory. English is my default language so what I need "en" directory to be hidden in the URL and at the same time redirect all the URL requests from root directory to "en" folder so they don't produce a 404 HTTP error. I partially achieved this with the following lines:
#Remove en/ directory from URL
RewriteRule ^$ en/
#Forward all the requests to en/ directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ en/$1
When requests are forwarded to "en" directory this is displayed again in the URL. This would be the flow:
User types: http://domain.com
index.php Redirects to http://domain.com/en because that's the detected language
.htaccess Rule hides /en directory, leaving the URL http://domain.com
Client types http://domain.com/panel to access their user panel
.htaccess Redirects to http://domain.com/en/panel
How can I remove "en" directory leaving the URL http://domain.com/panel after the last action in the list? Is there a better way to manage this behavior?
Current rules:
Options -Indexes +FollowSymlinks -MultiViews
ErrorDocument 404 /404.php
RewriteEngine On
RewriteRule ^/?$ en/ [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^((?!en/).*)$ en/$1 [L,NC]
#Prevent direct access to PHP Scripts
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule \.php$ - [R=404,L]
#Remove WWW
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
Have it this way:
Options -Indexes +FollowSymlinks -MultiViews
ErrorDocument 404 /404.php
RewriteEngine On
#Remove WWW
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L,NE]
# add a trailing slash if public/$1 is a directory
RewriteCond %{DOCUMENT_ROOT}/en/$1 -d
RewriteRule ^(.*?[^/])$ %{REQUEST_URI}/ [L]
#Prevent direct access to PHP Scripts
RewriteCond %{THE_REQUEST} \.php[?/\s] [NC]
RewriteRule ^ - [R=404,L]
RewriteRule ^/?$ en/ [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^((?!en/).*)$ en/$1 [L,NC]
Clear your browser cache and test your flows.
I just moved from a CentOS dedi to an Ubuntu VPS. The site is custom coded PHP.
Frontend works fine (including rewrite rules). The admin backend I can't get rewrite rules to work...
First error:
H00124: Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace.
Then after using debug level:
AH00122: redirected from r->uri = /admin/index.php
The relevant bits of my htaccess are:
# mod_rewrite set:
Options +Includes
RewriteEngine on
# Administration
RewriteCond %{REQUEST_URI} ^(/+)admin/(.*)$
RewriteRule (.*) %{DOCUMENT_ROOT}/admin/index.php [L,QSA]
# Rewrite orther
RewriteCond %{REQUEST_URI} !^(/+)index.php(.*)$
RewriteCond %{REQUEST_URI} !^(/+)syscmd.php$
RewriteRule ^(.*)$ %{DOCUMENT_ROOT}/index.php?page=$1 [L,QSA]
# If Rewriting Failure, Show error message (Internal backup)
RewriteCond %{REQUEST_URI} !^(/+)index.php$
RewriteCond %{REQUEST_URI} !^(/+)syscmd.php$
RewriteRule (.*) \1 [F]
This was working fine on CentOS too.
Any ideas? I already tried adding the following as the first condition:
RewriteCond %{REQUEST_URI} !/admin/ [NC]
That stopped it rewriting /admin completely.
Thanks
Your rules don't appear correct as %{DOCUMENT_ROOT} represents filesystem path and it shouldn't be used in target URLs.
Try these rules:
Options +Includes +FollowSymLinks
DirectoryIndex index.php
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
# Administration
RewriteRule ^admin/(.*)$ admin/index.php [L,NC]
# Rewrite other
RewriteCond %{THE_REQUEST} !/syscmd\.php [NC]
RewriteRule ^(.+)$ index.php?page=$1 [L,QSA]
I finally figured out how to get certain things to work on my website using the .htaccess file.
Redirect all non-www requests to www version. DONE.
Remove all php file extensions and add a trailing slash. DONE.
Prohibit directory views. DONE.
Limit caching. DONE.
Redirect 404 requests to home page. DONE.
This all seems to work well, but only in the ROOT directory.
It doesn't work well in subfolders. PHP extensions aren't removed. Folder paths in URLs disappear.
As I'm new to .htaccess files and regular expressions, and getting to this point took some time and lots of trial and error, I'm hesitant to tamper with the code any further.
I would appreciate any guidance on:
How to optimize this file for subfolders.
How to optimize this file in general.
Thank you.
RewriteEngine On
# redirect non-www requests to www version
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^example\.net [NC]
RewriteRule ^(.*)$ http://www.example.net/$1 [R=301,L]
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^example\.net [NC]
RewriteRule ^(.*)$ https://www.example.net/$1 [R=301,L]
# remove .php file extensions
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)/$ $1.php
# add trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
# disable directory view on web pages
Options -Indexes
# cached pages will expire in 5 days
ExpiresActive On
ExpiresDefault "access plus 5 days"
# re-direct 404 pages to home page
ErrorDocument 404 /
Keep your DocumentRoot/.htaccess like this:
# disable directory view on web pages
Options -Indexes
# cached pages will expire in 5 days
ExpiresActive On
ExpiresDefault "access plus 5 days"
# re-direct 404 pages to home page
ErrorDocument 404 /
RewriteEngine On
RewriteBase /
# redirect non-www requests to www (both http and https)
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTPS}s on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=302,L,NE]
# add trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
## hide .php extension
# To externally redirect /dir/file.php to /dir/file
RewriteCond %{THE_REQUEST} \s/+(.*?/)?(?:index)?(.*?)\.php[\s?] [NC]
RewriteRule ^ /%1%2/ [R=302,L,NE]
# To internally forward /dir/file to /dir/file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]
Once you verify it is working fine, replace R=302 to R=301. Avoid using R=301 (Permanent Redirect) while testing your mod_rewrite rules.
Im trying to make a redirect listen to a certain domain.
I have 3 domains;
- www.tocdesal.com
www.tocdesal.es
www.tocdesal.nl
Now i want achieve this..
www.tocdesal.com > www.tocdesal.com/en_GB/
www.tocdesal.nl > www.tocdesal.com/nl_NL/
www.tocdesal.es > www.tocdesal.com/es_ES/
Here is some code i use to manage multilanguage and ban people form certain folders.
I hope somebody can help me with this feature.
<IfModule mod_rewrite.c>
# Turn on URL rewriting
RewriteEngine On
# NON-WWW > WWW
#RewriteCond %{HTTP_HOST} ^www\.tocdesal\.nl [NC]
#RewriteRule (.*) http://www.tocdesal.nl/nl_NL/home/$1 [R=301,L]
# CUSTOM REWRITES
#Redirect 301 /[oldlink] [new-full-link]
# We dont want snooping people
Options -Indexes
#RewriteCond %{REQUEST_URI} ^framework.*
# If your website begins from a folder e.g localhost/my_project then
# you have to change it to: RewriteBase /my_project/
# If your site begins from the root e.g. example.local/ then
# let it as it is
#RewriteBase /tvmoordrecht
# Protect application and system files from being viewed when the index.php is missing
RewriteCond $1 ^(framework/modules|framework/coremodules|framework/cicore|application|assets|custommodules)
# Rewrite to index.php/access_denied/URL
RewriteRule ^(.*)$ index.php/access_denied/$1 [PT,L]
# Allow these directories and files to be displayed directly:
RewriteCond $1 ^(index\.php|robots\.txt|favicon\.ico|css|js|images|img|fonts|upload|framework/assets/|themes|framework/apps/elfinder|dev-cmslemonupdater|cmslemonupdater)
# No rewriting
RewriteRule ^(.*)$ - [PT,L]
# Rewrite to index.php/URL
RewriteRule ^(.*)$ index.php/$1 [PT,L]
</IfModule>
Insert these 3 rules right after RewriteEngine On line:
RewriteCond %{HTTP_HOST} ^(?:www\.)?tocdesal\.com$ [NC]
RewriteRule !^en_GB/ http://www.tocdesal.com/en_GB%{REQUEST_URI} [NE,R=301,L,NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?tocdesal\.nl$ [NC]
RewriteRule ^ http://www.tocdesal.com/nl_NL%{REQUEST_URI} [NE,R=301,L]
RewriteCond %{HTTP_HOST} ^(?:www\.)?tocdesal\.es$ [NC]
RewriteRule ^ http://www.tocdesal.com/es_ES%{REQUEST_URI} [NE,R=301,L]
This is what i have now. non-www redirects to www for the standard domain (.com) the rest redirects to the language on what the domain is set to.
<IfModule mod_rewrite.c>
# Turn on URL rewriting
RewriteEngine On
# Redirects to the page with that language selected
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^(?:www\.)?tocdesal\.nl$ [NC]
RewriteRule ^ http://www.tocdesal.com/nl_NL%{REQUEST_URI} [NE,R=301,L]
RewriteCond %{HTTP_HOST} ^(?:www\.)?tocdesal\.es$ [NC]
RewriteRule ^ http://www.tocdesal.com/es_ES%{REQUEST_URI} [NE,R=301,L]
# CUSTOM REWRITES
#Redirect 301 /[oldlink] [new-full-link]
# We dont want snooping people
Options -Indexes
#RewriteCond %{REQUEST_URI} ^framework.*
# If your website begins from a folder e.g localhost/my_project then
# you have to change it to: RewriteBase /my_project/
# If your site begins from the root e.g. example.local/ then
# let it as it is
#RewriteBase /tvmoordrecht
# Protect application and system files from being viewed when the index.php is mi
RewriteCond $1 ^(framework/modules|framework/coremodules|framework/cicore|applica
# Rewrite to index.php/access_denied/URL
RewriteRule ^(.*)$ index.php/access_denied/$1 [PT,L]
# Allow these directories and files to be displayed directly:
RewriteCond $1 ^(index\.php|robots\.txt|favicon\.ico|css|js|images|img|fonts|uplo
# No rewriting
RewriteRule ^(.*)$ - [PT,L]
# Rewrite to index.php/URL
RewriteRule ^(.*)$ index.php/$1 [PT,L]
</IfModule>