This question already has an answer here:
.htaccess exclude a directory to be rewrited
(1 answer)
Closed 9 years ago.
I have a general rewrite-rule in my sites root which looks like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([\w]+)/?([\w]+)? index.php?action=$1
which works fine for rewriting URLs like "www.example.com/myAction" to "www.example.com/index.php?action=myAction"
This must stay like this, but I need to exclude a subdirectory ("/login") from rewriting.
So I tried:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/login/
RewriteRule ^([\w]+)/?([\w]+)? index.php?action=$1
but still no luck. I've read several answers here on SO but can't find the flaw. Any ideas? Thank you!
You've an extra slash. .htaccess doesn't use the preceding slash unlike a Virtual Host. Also /login/ is a directory. So you're matching anything that is not a real file, but could potentially be a real directory. So if for instance '/css/' exists, you're rewriting it to index.php as an action on request.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^login/
RewriteRule ^([^/]+) index.php?action=$1
If it's still acting unexpectedly you might need to make sure you're not rewriting existing directories and try this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^login/
RewriteRule ^([^/]+) index.php?action=$1
I also noticed that you're only capturing up to the first slash (possibly) with your posted example code since you're using the ? look-up, so in my previous layout's I'm showing that code a little shorter. To capture the whole URI you would use this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^login/
RewriteRule ^(.*) index.php?action=$1
To capture a second (and third) string you can do something like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^login/
RewriteRule ^([^/]+)/([^/]+)/([^/]+) index.php?action=$1&page=$2&response=$3
What you have should work, but you can alternatively try to have all requests for /login/ be passed through instead:
RewriteEngine On
RewriteRule ^login/? - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([\w]+)/?([\w]+)? index.php?action=$1
Related
I am trying to figure out (learn) how to set up .htaccess so multiple Laravel installs could be run on the same IP by by using the URL (/site_1, /site_2) like:
http://xx.xx.xx.xx/site_1
So far what I have realized that subsequent request for resources (like: /css/app.css) don't pass the rewrite rules and are lost. I have tried the OR flag to tie the request somehow to the url but then rules gets mixed up.
How could I get this to work?
.htaccess
RewriteEngine ON
Options +FollowSymLinks
RewriteCond %{REQUEST_URI} /site_1 [NC,OR]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /site_1/public_html/public/$1 [L]
RewriteCond %{REQUEST_URI} /aloe [NC,OR]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /site_2/public_html/public/$1 [L]
Add this block and try again. Hope this will work. It might need some modification for the path you want to access for css and js.
RewriteBase /site_1/public_html/public/
RewriteCond %{REQUEST_FILENAME} \.(css|js)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /site_1/public_html/public/$1 [L]
first, i'm trying to find an answer to this like 3 nights :) If there's answer of this, i couldn't find or understand.
My question is simple:
I want to split get parameters with slashes. And use this globally in my script.
in htaccess, i wrote this;
RewriteRule ^(.*)/(.*) index.php?param1=$1¶m2=$2 [NC,QSA,L]
RewriteRule ^(.*) index.php?param1=$1 [NC,QSA,L]
but this blocks (or sth like that) css files. when browser loads page, css file links looks normal but browser can't reach them (because of htaccess gets directories as parameters)
Thank you for your helps
edit: my full .htaccess:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/(.*) index.php?param1=$1¶m2=$2 [NC,QSA,L]
RewriteRule ^(.*) index.php?param1=$1 [NC,QSA,L]
Make sure both your rules look like this. Conditions only work for the first rewrite rule following them.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*) index.php?param1=$1¶m2=$2 [NC,QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) index.php?param1=$1 [NC,QSA,L]
Also use absolute paths for your CSS files or put a / before the file path.
By putting this above the rules:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
It checks if the files already exist (eg the CSS files) and therefore won't try to rewrite or redirect them.
My .htaccess is
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^api\/(.*)$ api.php?action=$1 [L,QSA]
RewriteRule . index.php [L]
so all requests i receive in my index.php and then parse query string, and one of my pages is "api documentation page" with url http://domain.com/api so i require page about api from templates
and another url is http://domain.com/api.php where i need receive $_GET['action'] and another data with $_POST
so it will work like http://domain.com/api.php?action=start, but i need call it like http://domain.com/api/start or http://domain.com/api/start/ and receive $_GET['action'] - "start"
but this string in my .htaccess doesnt work
RewriteRule ^api\/(.*)$ api.php?action=$1 [L,QSA]
You need to keep RewriteCond before every RewriteRule if you want to apply for all URLs or keep them in a separate rule. Try this .htaccess:
Options -MultiViews
RewriteEngine On
RewriteBase /
# ignore all files/directories from all rules below
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^api/(.+)$ api.php?action=$1 [L,NC,QSA]
RewriteRule . index.php [L]
Ah, I've had that problem before, can't remember exactly how it went, but this link seems relevant: https://wiki.apache.org/httpd/RewriteFlags/QSA
The only difference with yours is the slash at the beginning to make the paths absolute and you don't need to escape slashes. E.g.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^api/([a-z]*)/? api.php?action=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
But none of those differences seem to be the cause of the problem. As pointed out by nubhava, the RewriteCond's only affect the next rule though.
When faced with this kind of situation though, I usually end up using a router, to do the "rewriting" in PHP.
See also Mod-Rewrite or PHP router?
RewriteEngine On
RewriteBase /
RewriteRule ^api/([^/]*)$ /api.php?action=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
work's, http://htaccess.madewithlove.be/ help's me trying different combinations
I'm trying to create shorter urls to some of my pages.
Previously I had a system that URLs were like /index.php?m=page&title=Page-Title
The piece of code I have now allows me to remove the everything 'm=' part. /index.php?m=page in here, the 'page' is a name of different modules, so this part might still change. I want to change the url only from 'page'.
How would I rewrite this so that my URL would be composed only as such:
/page=Page-Title
Would that be even possible since I'm using $_GET in 2 places?
My current .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/?p=$1 [L]
You can have your rules like this:
RewriteEngine On
RewriteRule ^page=([^/]+)/?$ /index.php?p=page&t=$1 [L,QSA,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/?p=$1 [L,QSA]
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
remove .php extension with .htaccess
I know little about .htaccess, What I'm trying to accomplish is two-fold.
Allow requests to .php to be called with or without the .php and
with or without a trailing slash. For example.
/profile.php works
/profile appends .php, works
/profile/ append .php so it works
If the file or directory doesn't exist after trying the above, then
treat as a 404 and redirect to another page.
/something failure, redirect to /404.php?id=$1 (url requested)
I have two rules that work separately, but I need to conditionalize these or something. Any advice would be appreciated.
RewriteEngine on
# This works for the PHP part.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)/?$ $1.php
# This works for general 404 handling
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /404.php?url=http://%{HTTP_HOST}/$1 [L]
Apache will automatically append .php to /profile and /profile/ without any mod_rewrite. The only thing you should handle is the 404 page.
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /404.php?url=http://%{HTTP_HOST}/$1 [L]
However, if it's not working for some reason (e.g. apache wasn't configured well):
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/?$ $1.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /404.php?url=http://%{HTTP_HOST}/$1 [L]