Why is .htaccess letting - .htaccess

I am having a bit of an issue with .htaccess. I want to stop mod rewrite taking effect if the directory is secure, or any sub directory/file of secure. I have set up .htaccess as follows:
RewriteEngine On
RewriteBase /
#skip next rule if url starts with secure/
RewriteRule ^/secure/(.*) - [L,S]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
<Files 403.shtml>
order allow,deny
allow from all
</Files>
Does anyone have any idea why it isn't working? I can't just turn off mod rewrite because I need to use htpassword for the directory.

In per-directory-context, the slash is appended to the end of the directory (leaving the part that rewriterule matches without a leading slash). Your first rule will therefore never match. In this case I think you can simply have a negative condition using %{REQUEST_URI}.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/secure/
RewriteRule ^(.*)$ index.php/$1 [L]

Related

How do I redirect base url to the specific one in .htaccess?

I would like to redirect my home page to /home. How can I do this?
RewriteRule ^[a-z]{0}$ /home //doesnt work
My whole .htaccess:
Options FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z]+)$ ?site=$1
Example of what do I need to achieve:
blue-world.pl > blue-world.pl/home > blue-world.pl?site=home
blue-world.pl/contact > blue-world.pl?site=contact
Based on your shown samples, could you please try following.
Please do make sure to clear your browser cache before testing your URLs.
Options FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(home|.*blue-world\.pl)/?$ ?site=$1 [L]
Explanation: Simply checking condition part while rewriting URL(home page) that if uri starts from either with / or without then rewrite it to /home. Also I am using L flag to stop this rule(rewriting rule) here. If this is the ONLY rule in your .htaccess file then you can change above L flag to END flag too. One more point, this considers that your home directory/folder is in root, if this is not the case then change from /home in above to home.
You may try these rules in your site root .htaccess:
DirectoryIndex index.php index.html
RewriteEngine On
# use ENV:REDIRECT_STATUS="" condition to avoid a redirect loop
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^$ /blue-world.pl/home [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .+ ?site=$0 [L,QSA]

htaccesc rewrite rule does not work

<Files ~ (\.php)>
Options +SymLinksIfOwnerMatch
</Files>
AddDefaultCharset utf-8
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php [NC,L]
RewriteRule ^([A-Za-z0-9-]+)/([0-9]+)/([0-9]+)$ index.php?lang=$1&menu=$2&submenu=$3 [L]
origin url: example.com/index.php?lang=de&menu=1&submenu=0
new url: example.com/de/1/0
new url load main page, but need another page
<Files ~ (\.php)>
Options +SymLinksIfOwnerMatch
</Files>
AddDefaultCharset utf-8
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php [NC,L]
RewriteRule ^([A-Za-z0-9-]+)/([0-9]+)/([0-9]+)$ index.php?lang=$1&menu=$2&submenu=$3 [L]
If you follow this, you told Apache to take everything that is not a file or a directory in the file system and rewrite it to index.php.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php [NC,L]
So that's what it is doing.
To make this do what you want, you have to make it like this:
<Files ~ (\.php)>
Options +SymLinksIfOwnerMatch
</Files>
AddDefaultCharset utf-8
RewriteEngine on
RewriteBase /
# if file/director does not exist, and if it matches the pattern
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-Za-z0-9-]+)/([0-9]+)/([0-9]+)$ index.php?lang=$1&menu=$2&submenu=$3 [QSA,L]
# then if it did not match the pattern, send it to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php [NC,L]
Note that you did not provide enough information to understand what you are actually trying to achieve. But my guess is it's something like this. I'm also assuming that you want to append the values to the query string of index.php (QSA), which is usually what you want.
But it's not really possible to guess further until you specify what all the urls are supposed to do.
Note that by matching anything not file/directory, and rewriting it without further argument as the last command to index.php (L), that ends the request processing, it will never hit the second case.

mod_rewrite: how to enable access to static content and redirect specific url schemes?

My apps' urls are like this: http://domain.com/projects/project-name/
My app's file system is like this:
htdocs/projects/project-name/
L index.html
L img/image1.png
L img/image2.png
L img/...
I need to send requests like http://domain.com/projects/project-name/ to
htdocs/index.php?section=$1&item=$2
But they display the project-name/index.html file instead.
Here is what i tried:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule . - [L]
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?section=$1&item=$2&%{QUERY_STRING} [NC,L]
Obviously, the RewriteCond %{REQUEST_FILENAME} -frule is the culprit. But i need to let the server serve the static img files. How can I tell it to let all file requests but rewrite the project folder's default index.html?
It should work, I am providing a slightly modified version of your rule with complete .htaccess to be placed in DOCUMENT_ROOT/.htaccess:
DirectoryIndex index.php index.html
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^([^/]+)/([^/]+)/?$ /index.php?section=$1&item=$2 [L,QSA]
Edit: Misunderstood the question.
You want to rewrite /projects/project-name/ to index.php?... I am also assuming you'll want to be able to request files from within project-name:
RewriteEngine on
RewriteRule ^([^/]+)/([^/]+)(/(index[^/]+)?)?$ /index.php?section=$1&item=$2 [QSA,NC,L]
If being able to request files from the project-name subdir isn't important, just remove (/(index[^/]+)?) and replace it with /. The (/(index[^/]+)?) assumes your DirectoryIndex files start with "index".
Meh.
Consider enabling the mod_rewrite log to analyze how it is applying your conditions and patterns. Within the server or vhost scope (not htaccess) add:
LogLevel warn rewrite:trace6
The LogLevel directive can enable logging for mod_rewrite which will be written to your defined ErrorLog. trace6 is the highest mod_rewrite log level before you get into hex output. Be sure to comment out or remove LogLevel when you're done.
Original, unrelated answer (ignore):
You need to negate files, of which right now you are not, notice the use of !:
RewriteCond %{REQUEST_FILENAME} !-f
Most importantly, your RewriteCond will not affect your second RewriteRule. It is only applied to the first RewriteRule (in your case RewriteRule . - [L]):
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?section=$1&item=$2&%{QUERY_STRING} [NC,L]
And you may also choose to negate directories:
RewriteCond %{REQUEST_FILENAME} !-d
I'd also consider using the QSA (query string append) flag instead of appending QUERY_STRING:
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?section=$1&item=$2 [QSA,NC,L]
In summary:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?section=$1&item=$2 [QSA,NC,L]

.htaccess rule not excluding existing files when rule has a slash condition

I am redirecting this type of url
http://www.site.com/windows/games/ or http://www.site.com/windows/games
http://www.site.com/windows/software/ or http://www.site.com/windows/software
to
http://www.site.com/windows/ct.php?ct=games
http://www.site.com/windows/ct.php?ct=software
Site structure
wamp/www/Site/windows/ct.php
I am trying this way its redirecting properly but when url has trailing slash at end its rewriting css, js files too.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^windows/([^/]*)/?$ /Site/windows/ct.php?ct=$1 [L]
i.e
this type of url has css and javascript applied.
http://www.site.com/windows/games
but this type of url has no css and javascript applied. (not working with trailing slash).
http://www.site.com/windows/games/
I have tried several syntax like
RewriteRule ^windows/([^/]+)/?$ /windows/ct.php?ct=$1 [L]
RewriteRule ^windows/(^(.[^/]*)/?)$ /windows/ct.php?ct=$1 [L]
RewriteRule ^windows/([^/]*)/? /windows/ct.php?ct=$1 [L]
RewriteRule ^windows/([^/]+)/? /windows/ct.php?ct=$1 [L]
but it didn't work.
Complete .htaccess
Options +FollowSymlinks -MultiViews
RewriteEngine on
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^windows/([^/]*)/?$ /Site/windows/ct.php?ct=$1 [L]
RewriteRule ^[^/]*/[^/]*/(.*\.html) /Site/error/400/ [L]
RewriteRule ^(error) - [L]
RewriteRule ^sitemap\.xml$ sitemap.php [L]
RewriteRule ^rss/(.*?)\.xml$ rss/$1.php [L]
</IfModule>
ErrorDocument 404 /Site/error/404/
Please see and suggest any possible way to do it.
Thanks.
The issue here is that your relative paths break when you add a trailing slash as it introduces two-levels of directory structure while the CSS/JS file paths come out only once ... So, your RewriteRule is actually firing for CSS/JS files as well when resolving 404s for paths like /windows/global/js/js.js. But, the scripts still fail to work because they aren't under /Site and that's where your rule is serving the request from.
So, to fix things without touching the relative URLs or resource locations; add another rule as follows:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^windows/([^/]*)/?$ /Site/windows/ct.php?ct=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^windows/(global/.*)$ /$1 [L] # handles css|js files
The rest of your .htaccess (of course) remains the same.
I use the following rule in .htaccess, but this may be too general:
RewriteRule ^q/([^/]+)/([^/]+)(\/?)$ /path/to/q.php?type=$1&q=$2&{QUERY_STRING} [NC]
This may be better for you:
RewriteRule ^windows(\/?)$ /path/to/windows.php?{QUERY_STRING} [NC]
RewriteRule ^windows/([^/]+)(\/?)$ /path/to/windows.php?type=$1&{QUERY_STRING} [NC]
You will have to change this according to your situation. I haven't tested this, but I hope it is good enough for you to get where you want.

Code Igniter SubDirectory Question

I am trying to let the "trac" directory and all of it's subdirectories be accessible through the url http://www.domain.com/trac/
I am working with the codeginiter framework and my directory structure looks like
.htaccess
index.php
system
trac
I can access the abov url fine, but the problem is the scripts and other files contained in trac subdirectories ie: trac/chrome/common/css/trac.css are not accessible and 404. Here is my .htaccess code. Please help.
RewriteEngine On
RewriteRule ^$ index.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond $1 !^trac/
RewriteRule ^trac/(.*) /trac/$1
You don't even need to mention /trac/ in your .htaccess. That's EXACTLY the point of
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
You're setting two "Rewrite Conditions." The first one says, "As long as the request isn't a file." The second one says "OR As long as the request isn't a directory."
Then
RewriteRule ^(.*)$ index.php?/$1 [L]
Sends everything else to index.php, where CI takes over. And just for the record, my full .htaccess that I use on every project:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
If you're confused about the RewriteCond %{REQUEST_URL} ^system.* line, it only exists so that any browser requests to the /system/ folder is always routed to index.php, and therefor ignored.
rewrite rules are executed in order... so try this
RewriteCond %{REQUEST_URI} !^/trac/
RewriteCond $1 !(index\.php/)
RewriteRule ^(.*)$ index.php?/$1 [L]
Explanation:
if the uri DOES NOT start with trac
if the uri IS NOT index.php
rewrite url as index.php?/{rest of the url}
If you remove the last two lines, it should work.
RewriteCond %{REQUEST_FILENAME} !-f checks to ensure that you're not requesting a file. If you are, this condition fails and the rule RewriteRule ^(.*)$ index.php?/$1 [L] is never executed.

Resources