.htaccess : remove index.php in the url if there is no querystring - .htaccess

I want to remove index.php in the url if there is no querystring.
My .htaccess is:
RewriteCond %{QUERY_STRING} ^$
RewriteRule index.php http://www.domain.fr [R=301,L]
RewriteRule ^(.*)/(.*)/$ index.php?category=$1&region=$2 [L,QSA]
www.domain.fr/index.php => ok, redirects well to www.domain.fr
www.domain.fr/index.php?i=1 => ok, no redirection
www.domain.fr/category/region/ (if entered manually in the url) => ok, no redirection
www.domain.fr/category/region/ (if posted via a php form) => redirect (and shouldn't) to www.domain.fr
Here is my php form:
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<select id="category" name="category">..</select>
<input id="send" name="send" type="submit" value="search" />
</form>
and the code of this form (at the top of the page):
if(isset($_POST['send'])){
$category = $_POST['category'];
$region = $_POST['region'];
header('Location: http://www.domain.fr/$category/$region/');
exit;
}
If I remove the cond RewriteCond %{QUERY_STRING} ^$ the php form redirects well to www.domain.fr/category/region, but if I put the cond it redirects always to www.domain.fr,
any idea ?

Your order of rules is the problem, keep your rules like this:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^domain\.fr$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=302,NE,L]
RewriteCond %{THE_REQUEST} /index\.php[^?] [NC]
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^index\.php$ / [R=302,L]
RewriteRule [A-Z] ${lc:%{REQUEST_URI}} [R=302,L]
RewriteRule .* - [E=SD:${vhost:%{HTTP_HOST}}]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?category=$1&region=$2 [L,QSA]
RewriteRule ^([^/]+)/?$ index.php?category=$1 [L,QSA]

Anubhava I've found the answer!!!
the problem was just about the php form, it's needed to remove:
<?php echo $_SERVER['PHP_SELF'];?>
The solution is simply:
<form method="post" action="">
but than kyou very much Anubhava!

Related

Htaccess for subdomain using Codeigniter

Can you help me with htaccess for sub domain using codeigniter.
In my localhost I have this htaccess that is working.
RewriteEngine On
RewriteBase /foldername/
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond $1 !^(index\.php|images|assets|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
Now if I move my files in my subdomain.sample.com, What should I write in my htaccess ?
In Config.php
# Localhost
$config['base_url'] = 'http://localhost/Appsite/website/';
# Live
$config['base_url'] = 'http://www.sample.com/'; // if secured site use https
# Subdomain
$config['base_url'] = 'http://subdomain.sample.com/';// if secured site use https
To accessing URL
click here
To access image
<img src="<?php echo base_url('assets/images/images.PNG');?>”>
To access css
<link href="<?php echo base_url('assets/css/style.css');?>" rel="stylesheet" />
To access js
<script src="<?php echo base_url('assets/js/some_js.js');?>"></script>

Htaccess doesn´t work - nice url

Hi guys i have problem with a htaccess.
I want from this URL domain.com/index?article=28&title=velky-biel-karlova-ves
(it doesn´t have index.php because i hide .php extension with htaccess.
I wanna have this URL domain.com/clanok/28/velky-biel-karlova-ves
I used this code
My htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^clanok/([\d]+)/([\w-]+)/?$ index.php?article=$1&title=$2 [NC,L]
RewriteBase /
## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
My index.php file:
<a href="index.php?article=<?php echo urlencode($row['id']); ?>&title=<?php echo createSlug($row['title']); ?>" title="">
It doesn´t work.
Thx for help.
Try it like this you still have to mention index.php your php hide rule is different from this,
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^clanok/([\d]+)/([\w-]+)/?$ index.php?article=$1&title=$2 [NC,L]
Please ensure this rule is not conflicting with your existing rules.

Removing index.php and another page inside the root folder from URL with .htaccess

In my Root folder I have index.php and profile.php.
Currently I'm using these rules for rewrite my index.php
from example.com/index.php?a=profile&u=user1 to example.com/profile&u=user1
RewriteEngine On
RewriteCond %{request_filename} -f
RewriteRule ^(.*) $1 [L]
RewriteRule ^(([^/]*)+)(/([^/]{0,32})(/.+)?)?$ index.php?a=$1&q=$3 [L]
RewriteRule ^welcome/([^/]+)/?$ index.php?a=welcome [NC]
RewriteRule ^page/([^/]+)/?$ index.php?a=page&filter=$1 [NC]
It works fine but now I want to rewrite my URL for profile.php page too, like this:
from example.com/profile.php?u=user1 to example.com/user1
How can I edit my .htaccess to rewrite profile.php together?
Based on question and comments above here are the rules that should work for you:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^profile/([^/]+)/?$ profile.php?u=$1 [NC,QSA,L]
RewriteRule ^welcome/([^/]+)/?$ index.php?a=welcome&filter=$1 [NC,QSA,L]
RewriteRule ^page/([^/]+)/?$ index.php?a=page&filter=$1 [NC,QSA,L]
RewriteRule ^(([^/]+)+)(/([^/]{0,32})(/.+)?)?$ index.php?a=$1&q=$3 [L,QSA]

.htaccess causing 403 - how can I fix this?

I have a website where I wrapped phpbb3 inside wordpress. It works pretty well, although when I added this security recommendation to my .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_METHOD} ^(HEAD|TRACE|DELETE|TRACK) [NC]
RewriteRule ^(.*)$ - [F,L]
RewriteCond %{QUERY_STRING} \.\.\/ [NC,OR]
RewriteCond %{QUERY_STRING} boot\.ini [NC,OR]
RewriteCond %{QUERY_STRING} tag\= [NC,OR]
RewriteCond %{QUERY_STRING} ftp\: [NC,OR]
RewriteCond %{QUERY_STRING} http\: [NC,OR]
RewriteCond %{QUERY_STRING} https\: [NC,OR]
RewriteCond %{QUERY_STRING} (\<|%3C).*script.*(\>|%3E) [NC,OR]
RewriteCond %{QUERY_STRING} mosConfig_[a-zA-Z_]{1,21}(=|%3D) [NC,OR]
RewriteCond %{QUERY_STRING} base64_encode.*\(.*\) [NC,OR]
RewriteCond %{QUERY_STRING} ^.*(\[|\]|\(|\)|<|>|ê|"|;|\?|\*|=$).* [NC,OR]
RewriteCond %{QUERY_STRING} ^.*("|'|<|>|\|{||).* [NC,OR]
RewriteCond %{QUERY_STRING} ^.*(%24&x).* [NC,OR]
RewriteCond %{QUERY_STRING} ^.*(%0|%A|%B|%C|%D|%E|%F|127\.0).* [NC,OR]
RewriteCond %{QUERY_STRING} ^.*(globals|encode|localhost|loopback).* [NC,OR]
RewriteCond %{QUERY_STRING} ^.*(request|select|insert|union|declare).* [NC]
RewriteCond %{HTTP_COOKIE} !^.*wordpress_logged_in_.*$
RewriteRule ^(.*)$ - [F,L]
</IfModule>
it is causing 403 errors when users try to use the search form on some of the forum pages. This is the way the search is presented (as an example):
<div class="search-box">
<form method="post" id="forum-search" action="./search.php?fid[]=5">
<fieldset>
<input class="inputbox search tiny" type="text" name="keywords" id="search_keywords" size="20" value="Search this forum…" onclick="if (this.value == 'Search this forum…') this.value = '';" onblur="if (this.value == '') this.value = 'Search this forum…';" />
<input class="button2" type="submit" value="Search" />
<input type="hidden" value="5" name="fid[]" />
</fieldset>
</form>
</div>
Comes back as "forbidden". When I take out that section of .htaccess, it works fine. I know the very basics about .htaccess and can't figure out why that code is causing a 403 on that section of HTML/PHP.
Any ideas would be appreciated...thanks...
It is causing a 403 because you are returning 403 in your htaccess, the F flag in your rewrite rules is going this.
There's probably a reason why you'd want to do this. The first rule blocks requests via HEAD, TRACE, DELETE, and TRACK methods. The second rule blocks requests with a bunch of possible query strings and *if you are missing the wordpress_logged_in cookie*. So if you don't have a cookie named wordpress_logged_in_, then you'll get a 403.

Delete ugly ?pg=1 from URL via .htacess RewriteRule

I have pagination plugin in my CMS which produces ?pg=1 links. I want to redirect this url without this ugly postfix because without it CMS shows first page too.
So, i have url like http://site.ru/category/schock-crane/?pg=1 or http://site.ru/moyka/?type=granit&pg=1
I want to redirect such urls to http://site.ru/category/schock-crane/ and http://site.ru/moyka/?type=granit respectly.
I tried this .htaccess code
RewriteRule (.*)(&|\?)pg=1$ $1 [R=301,L]
I tried this regexp code at regexp trainers - it worked. But at live server no redirect happens.
Here is whole .htaccess file:
AddDefaultCharset Off
#DirectoryIndex index.php index.html
Options +FollowSymLinks
Options -Indexes
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# fix /?pg=1
RewriteRule (.*)(&|\?)pg=1$ $1 [R=301,L]
# fix .palitra-tab
RewriteCond %{REQUEST_URI} -tab$
RewriteRule (.*)/([0-9]*)/\.(.*)-tab?$ http://site.ru/redirect.php?id=$2 [R=301,L]
RewriteCond %{REQUEST_URI} ^/csss/.* [NC]
RewriteRule csss/(.*)\.css$ css.php?n=$1 [L]
#RewriteRule ^(.*)/csss/(.*) /test.php [L]
RewriteRule ^(.*)/base([0-9]+)/?$ $1?base=$2
#RewriteCond %{REQUEST_FILENAME} -f
#RewriteRule \.(gif|jpeg|jpg|png)$ /images/watermark/index.php [L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+) - [PT,L]
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*) index.php
RewriteCond %{HTTP:Authorization} !^$
RewriteRule .* - [E=REMOTE_USER:%{HTTP:Authorization}]
</IfModule>
You need to use RewriteCond to examine the query string:
RewriteCond %{QUERY_STRING} ^(([^&]*&)*)pg=1(&(.*)|$)
RewriteCond %1%4 (.*?)&?$
RewriteRule ^ %{REQUEST_URI}?%1 [R=301,L]
The second RewriteCond is just to remove a trailing &.
Maybe the missing ^ or / kills the redirect
# fix /?pg=1
RewriteRule ^(.*)(&|\?)pg=1$ /$1 [R=301,L]
or try
RewriteRule ^(.*)(&|\?)pg=1$ http://%{HTTP_HOST}/$1 [R=301,L]

Resources