I'm looking into setting up an application where there is a core and a project namespace, where core is the default fallback to the project customisation. to this end id like to be able to cascade various resources like css, javascript etc. for the purposes of the excersize, ive simplified this as
./.htaccess
./first/firstonly.txt
./first/both.txt
./second/secondonly.txt
./second/both.txt
expected behaviour would be a request would check for existence in first before looking in second, and finally throwing 404.
for baseurl/firstonly.txt would hit ./first/firstonly.txt (200),
whereas baseurl/secondonly.txt would try ./first/secondonly.txt (404) then ./second/secondonly.txt (200).
baseurl/both.txt would hit ./first/both.txt (200) and go no further.
baseurl/nonexistant.txt (404) would run through the cascades and return 404.
I'm fairly competant with mod_rewrite, so dont feel the need to talk basics here. What would be the most efficient (sane) way of implementing this? Speed concerns aside, as most of the time things will be found on the first hit.
Try these rules:
RewriteEngine on
RewriteRule ^(first|second)/ - [L]
RewriteCond %{DOCUMENT_ROOT}/first/$0 -f
RewriteRule ^[^/]+$ first/$0 [L]
RewriteCond %{DOCUMENT_ROOT}/second/$0 -f
RewriteRule ^[^/]+$ second/$0 [L]
Some other potential solutions
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^first
RewriteCond %{REQUEST_URI} !^second
RewriteRule ^([^/]+\.?[^/])*$ first/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^second
RewriteRule ^first/([^/]+\.?[^/])*$ second/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^(first|second).*$
RewriteRule .* /404.html [L]
or
RewriteCond %{REQUEST_URI} 404.html
RewriteRule .* - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^first
RewriteCond %{REQUEST_URI} !^second
RewriteRule ^([^/]+\.?[^/])*$ first/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^second
RewriteRule ^first/([^/]+\.?[^/])*$ second/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^(first|second).*$
RewriteRule .* /404.html [L]
In my personal testing im noticing that the -f check is accurate only against a full pathname, which makes things tricky if you're not in the document root. Could this be addressed by rewriting to the file, and then doing the -f on the request_uri?
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
RewriteRule ^Public/(.*)$ Application/Public/$1 [L]
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]
RewriteRule ^(css|scripts|images|assets|flash)/(.*)$ Public/$1/$2 [L]
This is the final working solution (outside the sandbox example above).
Related
like a title i have a problem with this rewrite rule
I write this
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !\.(font|css|js|images|remote) [NC]
RewriteCond %{REQUEST_URI} !^/(font|css|js|images|remote/.*)$ [NC]
RewriteRule ^index$ /page/index [QSA,L]
RewriteRule ^(.*)/(.*)$ /page/article&category=$1&post=$2 [QSA,L]
When i call ajax remote from /remote/check-function.php from pages he load full /page/post with variable sent by ajax.
How i can resolve this issue
Thanks in advance.
Best.
The RewriteCond only applies to the RewriteRule that comes immediately. Properly that the url /remote/check-function.php matches the last rule and is rewritten. You should add RewriteCond to check whether the request matches the existing files / directories before applying the last rule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*)$ /page/article&category=$1&post=$2 [QSA,L]
I have the following .htaccess:
RewriteCond %{SERVER_PORT} !^443$
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [C]
RewriteCond %{REQUEST_FILENAME} ^(.*).php$
RewriteRule ^(.*)$ /index.php [NC,L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
I want it to firstly reroute everything to HTTPS.
Then I want to check if the user is accessing a PHP file, such as domain.com/pages/page/a.php and route that to /index.php.
Then lastly I want everything thats not a file or a directory to route to index.php such as domain.com/my-fun-page
I think the first and the last blocks are correct, but for some reason when I run a test on htaccess tester It works successfully. However when I run it on my own server it still lets me access domain.com/pages/page/a.php.
How can I find out why, or what am I doing wrong?
Try with below for php part,
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^ /index.php [NC,L,QSA]
You can combine your 2nd a 3rd rule into a single rule like this:
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,LE]
RewriteCond %{REQUEST_FILENAME}\.php -f [OR]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
Even RewriteCond %{REQUEST_FILENAME}\.php -f [OR] may not be necessary as !-f and !-d will already cover this case.
I've got for example this url:
http://example.com/team
and I want during this specific time for example: 00:00 to 00:05 (for 5 minutes) to restrict access to it and instead when trying to load /team to be redirected to /home
BTW: I am using codeigniter framework.
This is my current htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/system.*
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?/$1 [L]
I tried with ...
....
RewriteCond %{TIME_HOUR}%{TIME_MIN} >0000
RewriteCond %{TIME_HOUR}%{TIME_MIN} <0005
....
but no luck. Probably I am doing it wrong but I don't get it how it should redirect /teams and such urls?
Giving the other presents rules in your file, you would probably want:
RewriteCond %{TIME_HOUR}%{TIME_MIN} >0000
RewriteCond %{TIME_HOUR}%{TIME_MIN} <0005
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+team [NC]
RewriteRule ^ /home? [R=307,L]
Your current file would look like:
RewriteEngine On
RewriteCond %{TIME_HOUR}%{TIME_MIN} >0000
RewriteCond %{TIME_HOUR}%{TIME_MIN} <0005
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+team [NC]
RewriteRule ^ /home? [R=307,L]
RewriteCond %{REQUEST_URI} ^/system.*
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?/$1 [L]
I don't see anything else wrong with your condition it looks just fine and I tested it myself.
The only factor I could see affecting it would be that its using the server time rather than your local time, unless the server is on your computer/local network/local time.
I think you area looking for this ...
RewriteCond %{TIME_HOUR} 00
RewriteCond %{TIME_MIN} <05
RewriteRule ^team /home [R=307,L]
Try putting one after the other, like this:
RewriteCond %{TIME_HOUR} >00
RewriteCond %{TIME_MIN} < 5
RewriteRule ^dream/?$ /promo.php [L]
See this page:
http://www.askapache.com/htaccess/time_hour-rewritecond-time.html
I have to completely replace an existing website. The current site is completely spagetti code with some rewrite rules to mimic friendly urls.
There are some mission critical issues that can't be resolved with the current architecture and database structure, so for a time both code bases need to live side by side.
This is the current .htaccess
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^([^/]+)/$ $1.html
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ $1.php?q=$2
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/$ $1.php?q=$2&r=$3
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/$ $1.php?q=$2&r=$3&s=$4
I'm replacing it with a zend framework site, but it routes everything through the index.php file for the routing
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f [NC,OR]
RewriteCond %{REQUEST_FILENAME} -d [NC]
RewriteRule .* - [L]
RewriteRule .* index.php
Does anyone have any idea how I can combine the two until the original can be completely replaced?
If you can map your old URLs to Zend Url someway, then it would be rather easy. Let's say you had
http://foo.bar.com/something.php?q=arg1&r=arg2
then if you have this functionality working in Zend code, then you must also have valid URL there, like
http://foo.bar.com/something/q/arg1/r/arg2
If so, all you need is to rewrite that old URLs to Zend URL and instead of doing internal redirection, do HTTP redirection, i.e. (out of my head, not tested):
RewriteCond %{QUERY_STRING} ^q=(.*)&r=(.*)^
RewriteRule ^something\.php$ /something/q/%1/r/%2? [R=301,L]
Mind the trailing "?" which tells mod_rewrite NOT to attach original query string to rewritten one (otherwise you would end with /something/q/%1/r/%2?q=X&r=y. Note we do regular 301 HTTP redirection here.
They're not going to merge very easily, it's a matter of which one you want to have precedence. You could add a few more conditions to make sure everything doesn't get routed as a $1.php file, then add the zend rules to the end.
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^([^/]+)/$ $1.html
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)/$ $1.php
RewriteCond %{REQUEST_URI} ^([^/]+)/
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^([^/]+)/([^/]+)/$ $1.php?q=$2
RewriteCond %{REQUEST_URI} ^([^/]+)/
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/$ $1.php?q=$2&r=$3
RewriteCond %{REQUEST_URI} ^([^/]+)/
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/$ $1.php?q=$2&r=$3&s=$4
# other rules
RewriteCond %{REQUEST_FILENAME} -f [NC,OR]
RewriteCond %{REQUEST_FILENAME} -d [NC]
RewriteRule .* - [L]
RewriteRule .* index.php
I'm trying to build a short URI service with CI just so I can learn CI faster
anyway .. I got stuck at the routing
i hid the index.php then added the following route $route['([A-z0-9]{4})'] = "/forward/redirect/$1";
but it just shows my default controller
I also tried with HTaccess
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-z0-9]{4})$ /forward/redirect/$1 [NC]
It gives an error for not having any passed data
any help is appreciated.
Cheers
Since there is no physical path /forward/redirect/, you should redirect to the "catch all" index.php file in the root and input the path as parameter:
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-z0-9]{4})$ /index.php/forward/redirect/$1 [NC]
Or you can leave the rule as is and append another rule (this way you will have two rewriting cycles, the first one will rewrite to /forward/redirect/asdf and then the second one rewrites to index.php/forward/redirect/asdf:
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-z0-9]{4})$ /forward/redirect/$1 [NC]
RewriteRule ^(.*)$ index.php/$1 [QSA,L]
i finally got it working >_>
routes file contains this
$route['([A-z0-9]{4})'] = "/forward/redirect/$1";
htaccess contains this
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*) http://%1/$1 [R=301]
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-z0-9]{4})$ index.php/forward/redirect/?$1 [NC]
RewriteCond $1 !^(index\.php|images|robots\.txt|(.*).js|(.*).css|(.*).jpg|(.*).png)
//added (.*) so resources could be loaded properly :)
RewriteRule ^(.*)$ /index.php/$1 [L]