Keep doubles slashes in mod_rewrite - mod-rewrite

I try to rewrite urls like:
http://www.example.com/contoller/method/var1/var2/var3
to
http://www.example.com/index.php?q=contoller/method/var1/var2/var3
My rewrite rules are as below:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
It works well except with doubles slashes. Indeed, there are replaced by singles slashes.
I would like to be able to use it when a var is empty. For example, if the var2 is empty the URL contains a double slashes on the var2 location

Just use
RewriteRule ^ index.php [L]
The URL you're corrupting in the query string is already available within index.php as $_SERVER['REQUEST_URI'].

Related

.htaccess - Check .jpg size and replace if match

I need to control all remotes .jpg matching sizes: 4456 and 4824. If match these bytes I need to replace with a default image. The below code is to replace a .jpg if doesn't exist.
RewriteCond %{REQUEST_URI} \.(jpeg|JPEG|jpg|JPG)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .*$ /default-image.jpg [L]
I need that if a example.com/*.jpg have size (4456 | 4824) then replace with the default-image.jpg.
In PHP this is working like a charm for my needs but I would prefer to set in .htaccess.
$link = 'https://example.com/test.jpg';
$check_image = get_headers($link, 1);
$bytes = $check_image["Content-Length"];
if ($bytes < 5000) { ...
You can do the following on Apache 2.4 using an Apache expression with mod_rewrite:
RewriteCond %{REQUEST_URI} !^/default-image\.jpg$
RewriteCond expr "filesize(%{REQUEST_FILENAME}) =~ /4456|4824/"
RewriteRule \.jpe?g$ /default-image.jpg [NC,L]
If the requested image does not exist then the filesize() function simply returns 0 (no error). So, to handle non-existent images as well, then you could just change the regex to include 0, ie. /0|4456|4824/.
Aside:
RewriteCond %{REQUEST_URI} \.(jpeg|JPEG|jpg|JPG)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .*$ /default-image.jpg [L]
In the first condition, there is no need to check jpeg|JPEG since you are using the NC (case-insensitive) flag on the directive. However, it is more optimal to perform this check in the RewriteRule pattern instead, rather than creating an additional condition.
There's no need to check that the request does not map to a directory (3rd condition), unless you also have directory names that end in .jpg etc. (unlikely). Filesystem checks are relatively expensive, so it is best to avoid them if possible.

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]

Rewrite rules confused

I am struggling to achieve this simple thing...
I have some static pages which should be like
www.domain.com/profile etc..
The problem is how to write the rewrite rules in order to ..
There would be some fixed rewrites
like /home
I want every file that exists not to be rewritten
www.domain.com/test.php should go to
test.php
Lastly if it is not found i want it to be redirected to static.php?_.....
RewriteRule ^/home/?$ /index.php?__i18n_language=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/([^/]+)/?$ /static.php?__i18n_language=$1
This works ok but if i type index.php or test.php or even the mach from other redirection it gets me in static.php...
Please help!
According to your description you can use these rules:
# stop rewriting process if request can be mapped onto existing file
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteRule ^ - [L]
# rewrite known paths /home, /foo, /bar, etc.
RewriteRule ^/(home|foo|bar|…)$ /index.php [L]
# rewrite any other path
RewriteRule ^ /static.php [L]
I haven't used this in a long time, but it's something I found, that should help. It is part of an old script that generates .httaccess files for redirecting from /usr/share/doc only when the doc isn't found:
The rule is "Check, and if the target url exists, then leave":
RewriteEngine On
RewriteBase /doc/User_Documents
### If the directory already exists, then leave
### We're just redirecting request when the directory exists but has been renamed.
RewriteCond %{REQUEST_FILENAME} User_Documents/([^/]+-[0-9][^/]*)
RewriteCond $PWD/%1 -d
RewriteRule .* - [L]
It's the [L] that means leave if one of the conditions is matched. In the old script, there are hundreds of generated rules (after [L]) that are skipped, because one of the conditions matched. In your case you would skip the rest of the rules when the target %{REQUEST_FILENAME} is found.
So, I suggest, before the redirection rule:
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [L]

.htaccess and trailing slash in CodeIgniter

In my config file, I have:
$config['url_suffix'] = "/";
Here is my .htaccess:
Options +FollowSymLinks
Options -Indexes
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|images|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^.+[^/]$
RewriteRule ^(.+)$ $1/
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
When I load a page, I don’t get the trailing slash. I am using URI routing, but even when I go to the actual path of the controller/view, I still don’t get the trailing slash.
What am I doing wrong? I tried removing the slash after the .php in the last line of the .htaccess, but that didn’t do it.
Even if I add “.html” as my URL suffix, it doesn’t get added. And that’s not in my .htaccess.
If I try to make $route['news-and-events'] = "news"; this instead: $route['news-and-events/'] = "news"; I get a 404 error
EDIT: With the above .htaccess I get an error that I am using disallowed characters, even when I add "/" to my allowed characters string in the config file.
Found a one that worked for me, Imported my Blogger posts to my Wordpress, and then my back links didn't work, so I just replaced my .htaccess with this example, check it out!
http://www.high-on-it.co.za/2011/02/removing-trailing-html-from-url/
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} \.html$
RewriteRule ^(.*)\.html$ $1 [R=301,L]
RewriteRule ^index\.php$ – [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

Domain name in URL as variable

My .htaccess file has the following code:
RewriteEngine on
RewriteRule ^/?(.*)$ index.php?domain=$1 [L]
I'm trying to get domain names as variables from URLs like:
hxxp://www.example.com/www.domain.name or
hxxp://www.example.com/subdomain.domain.name or
hxxp://www.example.com/domain.name
but with $_GET['domain'] my variable is always 'index.php' and not the domain names.
With hxxp://www.example.com/domain/www.domain.name and .htaccess code
RewriteEngine on
RewriteRule ^domain/?(.*)$ index.php?url=$1 [L]
everything is OK, but I would like to remove the 'domain/' part from the URLs.
I've searched for this, but couldn't find anything. Could someone please help me with this?
something like
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php?domain=$1 [L]
in this case $1 will be:
http://site/www.example.com $1 = www.example.com
http://site/www.example.com/xyz $1 = www.example.com/xyz
Try this rule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^[^/.](\.[^/]+)+$ index.php?domain=$0 [L]
This will rewrite any request with a URL path that contains at least one dot (foo.bar, foo.bar.baz, etc.) to your index.php.

Resources