Rewrite redirecting first to cached content - caching

I tried many combinations but none works. I trying to write rewrite rule which:
Assume URL of http://example.com/project/big_bunny
When user visit site rewrite first try to read file /cache/project/big_bunny.html but only if there is no any get or post data
If there is no cache rewrite process standard router rule: index.php?_rt=$1 [L,QSA]
Any suggestions?

Try this:
# Check if using POST method
RewriteCond %{THE_REQUEST} !^POST
# Check if already rewrote to cache
RewriteCond %{REQUEST_URI} !^/cache
# Check if cache exists
RewriteCond /cache%{REQUEST_URI}.html -f
# Check if contains any GET query string
RewriteCond %{QUERY_STRING} !.+
# Do the rewrite
RewriteRule ^/(.*) /cache/$1.html [L]
# Check if already rewrote to cache
RewriteCond %{REQUEST_URI} !^/cache
RewriteRule ^/(.*) index.php?_rt=$1 [L,QSA]

Try this:
# Check the cache, and rewrite if file exists
RewriteCond %{THE_REQUEST} !^POST [NC]
RewriteCond %{QUERY_STRING} ^$
RewriteCond %{DOCUMENT_ROOT}/cache%{REQUEST_URI}.html -f
RewriteRule ^ /cache%{REQUEST_URI}.html [L]
# If URI doesn't start with /cache/, it wasn't cached so rewrite to index.php
RewriteCond %{REQUEST_URI} !^/cache/
RewriteRule ^(.*)$ index.php?_rt=$1 [L,QSA]

Related

Mod_Rewrite: Rewrite URL to index.php but redirect index.php to root

I want to rewrite requests to use index.php which is pretty standard and works.
The problem is that there are old and now invalid links which include the index.php and I want to redirect them to the root url of my site.
Potential duplicates include: Rewrite URL to index.php but avoid index.php in the URL, but the answers mentioned don't work for me.
RewriteEngine on
# This results in a redirect loop
#Redirect 301 /index.php http://domain.com
# This gets me a 500 error
# RewriteCond %{THE_REQUEST} ^GET\ /index\.php/?([^ ]*)
# RewriteRule ^index\.php/?(.*) / [L,R=301]
# Push www traffic to base domain. <<= Works
RewriteCond %{HTTP_HOST} !^domain.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [L,R=301]
# Final rewrite to include index.php <<= Works
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule /?([A-Za-z0-9_-]+)/?$ index.php?id=$1 [QSA,L]
The url: http://domain.com/index.php/component/fsf/?view=faq, should be invalid and it should return a 404 error but because of my current rewrite rules, a site visitor will see the main page. I want to redirect this with a 301 to the main page so it can be dropped from search indexes.
Any help would be appreciated.
Change your THE_REQUEST rule to
RewriteCond %{THE_REQUEST} ^GET\ /index\.php(/.*) [NC]
RewriteRule ^ /? [L,R=301]

htaccess file for codeigniter on subdomain virtualhost

i have problem about .htaccess file for my codeigniter installation.
On localhost this work fine, when i upload online its rewrite URL but the website not work.
this is my htaccess on localhost:
RewriteEngine On
RewriteBase /jCore_01/
#RewriteBase /
### Canonicalize codeigniter URLs
# If your default controller is something other than
# "welcome" you should probably change this
RewriteRule ^(welcome(/index)?|index(\.php)?)/?$ / [L,R=301]
RewriteRule ^(.*)/index/?$ $1 [L,R=301]
# Removes trailing slashes (prevents SEO duplicate content issues)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ $1 [L,R=301]
# Enforce NO www
#RewriteCond %{HTTP_HOST} ^www [NC]
#RewriteRule ^(.*)$ http://domain.tld/$1 [L,R=301]
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
online i have a problem:
i put this file on subdomain jcore.miodomio.com and it rewrite URL but not work
example:
without htaccess it's work:
jcore.miodominio.com/index.php/en/home
with htaccess
jcore.miodominio.com/en/home
Not Found
The requested URL /en/home was not found on this server.
can anyone help me? suggestion?
regards
Denny
Try change your htaccess to:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
Somethings to consider.
Did you enable Apache rewrite module?
If enabled check whether your virtual host file is configured properly So to make sure its hitting .htaccess or try putting some dump on .htacess , So if its hitting .htaccess than you should get an 500 server error.
If you are not getting error than its mostly the problem in your host settings.

rewrite condition not working after stripping query string

I am using iirf on IIS6 on a Windows 2003 server. We have the following code to present clean urls and to remove any query strings:
RewriteRule ^/(.+)\?(.+)&(.+)\.(htm)$ /$1
RewriteRule ^/(.+)\?(.+)$ http://betatest.bracknell-forest.gov.uk/$1 [R=301]
# Redirect to ASP if it exists.
# e.g. example.com/foo will display the contents of example.com/foo.asp
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.asp -f
RewriteRule ^(.+)$ $1.asp [L,QSA]
RedirectRule ^/(.+)\.(asp)$ http://betatest.bracknell-forest.gov.uk/$1 [R=301]
# Any bare URL will get rewritten to a URL with .htm appended
RewriteRule ^/([\w]+)$ /$1.htm [I,L]
RedirectRule ^/(.+)\.(htm)$ http://betatest.bracknell-forest.gov.uk/$1 [R=301]
The problem is that since I have added
RewriteRule ^/(.+)\?(.+)&(.+).(htm)$ /$1
RewriteRule ^/(.+)\?(.+)$ http://betatest.bracknell-forest.gov.uk/$1 [R=301]
All query string parameters are removed where we actually need them for(sorry - urls not available externally):
betatest.bracknell-forest.gov.uk/news.asp?page=2
In fact the only query string condition we want to to strip the parametrs for is if it contains the Facebook parameter fb_action_ids=52352315213 e.g.
betatest.bracknell-forest.gov.uk/help?fb_action_ids=372043216205703&fb_action_types=og.likes&fb_source=aggregation&fb_aggregation_id=288381481237582
I have tried using:
RewriteCond %{QUERY_STRING} ^ fb_action_ids=(.)$ [I]
before the first pair of rules but but it does not appear to do anything.
Just managed to resolve it and boy it feels like a burden off my shoulders:
#key thing I have changed is to specify the query string parameters fb_action_ids and fb_source
RewriteRule ^/(https?)://([^/]+)(/([^\?]+(\?(.*))?)?)? /$1
RewriteRule ^/(.+)\?(fb_action_ids=(.*)|fb_source=(.*))$ http://betatest.bracknell-forest.gov.uk/$1 [R=301]
# e.g. example.com/foo will display the contents of example.com/foo.asp
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.asp -f
RewriteRule ^(.+)$ $1.asp [L,QSA]
RedirectRule ^/(.+)\.(asp)$ http://betatest.bracknell-forest.gov.uk/$1 [R=301]
# Any bare URL will get rewritten to a URL with .htm appended
RewriteRule ^/([\w]+)$ /$1.htm [I,L]
RedirectRule ^/(.+)\.(htm)$ http://betatest.bracknell-forest.gov.uk/$1 [R=301]

Why subdomain rewrite does not work?

I want my subdomain to point directly to a folder.
I have found the following mod_rewrite script to setup this behavior.
Unfortunately it does not work.
When I navigate to fish.example.com, the browser displays 404 error with the following message.
"The requested URL / was not found on this server."
Do you know why?
How can I make it work?
# Internally rewrite <subdomain>.example.com/<URLpath> to example.com/subs/<subdomain/<URLpath>
RewriteEngine on
RewriteCond $1 !^fish/
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
RewriteRule (.*) /fish/%1/$1 [L]
UPDATE
I have changed the script to the following, but in this case the browser redirects to example.com instead of example.com/fish
Do you know why?
RewriteCond $1 !^fish/
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
RewriteRule ^fish/(.*) /fish/%1/$1 [L]
Try this:
RewriteEngine On
Rewrite Base / # Make sure it starts from the domain
RewriteCond %{HTTP_HOST} ^fish.example.com [NC] # Catch your subdomain
RewriteRule (.*)$ fish/$1 [L] # Reroute to folder.
Update:
I see... try this:
RewriteCond $1 !^fish/
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
RewriteRule (.*) http://example.com/%1/$1 [R=301,L]
Basically a 301 redirect... %1 matches the subdomain in the previous RewriteCond, and $1 matches the original url

How do I write a custom mod_rewrite .htaccess file for CakePHP routing?

I've rewritten my web app using CakePHP, but now I need to have my old formatted urls redirect to my new url format. I can't seem to add my own custom mod rewrite rule. I've added it above the main cakephp rewrite rule, but I'm getting an infinite redirect loop. I just want http://mysite.com/index.php?action=showstream&nickname=user to redirect to http://mysite.com/user before the cakephp rewrite happens.
EDIT: Ok, so now when the condition is met it's redirecting but it's appending the original query string to the end. I'm assuming that's due to the QSA flag in CakePHP rewrite rules, but I was under the impression the "L" in my rule would stop that from executing...
RewriteEngine On
RewriteCond %{QUERY_STRING} ^action\=showstream&nickname\=(.*)$
RewriteRule ^.*$ http://mysite.com/%1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
When you do a capture inside the RewriteCond line instead of the RewriteRule, you have to reference the capture with %N instead of $N. That is, your RewriteRule line should be:
RewriteRule ^index.php$ /%1 [R=301,L]
Try to test the request line (THE_REQUEST) to see what URI originally has been requested:
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /index\.php
RewriteCond %{QUERY_STRING} ^action=showstream&nickname=([^&]*)$
RewriteRule ^index\.php$ /%1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* index.php?url=$0 [QSA,L]
But maybe it would be easier to do this with PHP.

Resources