Removing GET parameter in .htaccess - regex

I'm not able to remove one GET parameter wrongly added by one 404 SEF module.
I want to remove "task=view" only if it appears alone and not with another parameter.
So
www.mysite.com/1.html?task=view should be redirected to www.mysite.com/1.html.
While www.mysite.com?task=view&view=article should remain unchanged.
No matter which RewriteRule I use, this paramater does not go. Looks like it is being generated from any environment variable when index.php is run.
For example this does not work:
RewriteCond %{QUERY_STRING} ^task=view$ [NC]
RewriteRule (.*)task=view.* $1 [R=301,L]
Here is my .htaccess file:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{QUERY_STRING} mosConfig_[a-zA-Z_]{1,21}(=|\%3D) [OR]
RewriteCond %{QUERY_STRING} base64_encode.*(.*) [OR]
RewriteCond %{QUERY_STRING} (<|%3C).*script.*(>|%3E) [NC,OR]
RewriteCond %{QUERY_STRING} GLOBALS(=|[|\%[0-9A-Z]{0,2}) [OR]
RewriteCond %{QUERY_STRING} _REQUEST(=|[|\%[0-9A-Z]{0,2})
RewriteRule ^(.*)$ index.php [F,L]
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/index.php
RewriteCond %{REQUEST_URI} (/|.php|.html|.htm|.feed|.pdf|.raw|/[^.]*)$ [NC]
RewriteRule (.*) index.php
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]
Options -Indexes
What is the way to handle this?

Use this rule:
RewriteCond %{QUERY_STRING} ^task=view$ [NC]
RewriteRule ^(.*)$ $1? [R=301,L]
? at the end of $1 will strip-off any existing query string.

Related

.htaccess Internal Redirect issue

Something with my .htaccess file is creating a rule loop of some sort when a certain condition exists. Here is the code in question:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$ [NC]
RewriteRule ^(.+)/?$ /%1.php?o=$1 [L,NC,QSA]
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com$ [NC]
RewriteRule ^$ /%1.php [L,NC,QSA]
</IfModule>
Normally what this does is:
test.example.com -> example.com/test.php
test.example.com/test2 -> example.com/test.php?o=test2
Thats fine, except when the "test" subdomain part points to a nonexistent file, I get an internal redirect error, specifically this:
Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace., referer: [retracted]
Any help is appreciated.
Yes it will cause looping because RewriteCond %{REQUEST_FILENAME} !-f will still be true for /test.php when /test.php doesn't exist.
You can use this code to prevent looping:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
# don't redirect after one internal redirect
RewriteCond %{ENV:REDIRECT_STATUS} .+
RewriteRule ^ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$ [NC]
RewriteRule ^(.+?)/?$ /%1.php?o=$1 [L,QSA]
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$ [NC]
RewriteRule ^$ /%1.php [L]
</IfModule>

url rewriting in .htaccess

I am trying to serve different urls using mod_rewrite but whatever I try it is just not working.
An example url would be
http://www.site.com/country/tours/dynamic-part/?&city=new-york,los-angeles
And I am trying to change the url using .htaccess to:
http://www.site.com/country/tours/dynamic-part/new-york,los-angeles
RewriteEngine on
RewriteBase /
RewriteCond %{QUERY_STRING} (^|&)city=([^&]*)(&|$)
RewriteRule ^country\/tours\/([a-zA-Z0-9]*)\/.+city=([^\/]*)$ http://www.site.com/country/tours/$1/$2 [L,R=301]
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
Any ideas? I though I was close but not anymore :/
The RewriteRule does NOT match the query string, see
http://httpd.apache.org/docs/current/mod/mod_rewrite.html#what_is_matched
So the .+city part of the rule will never match.
This should work tho...
RewriteCond %{QUERY_STRING} (^|&)city=([^&]*)(&|$)
RewriteRule ^country\/tours\/([a-zA-Z0-9]*)\/ http://www.site.com/country/tours/$1/%2 [L,R=301]
The subsitution can read back-referenecs to the RewriteCond pattern.

htaccess rewriterule redirect everypage to a single page

I want to redirect every url to a single php file that will act as a dispatcher that will pull content from a DB based on the url. I am unsure what is wrong with this. Any pointers please.
<IfModule mod_rewrite.c>
AddDefaultCharset utf-8
rewriteCond %{REQUEST_URI} !(^/admin/)
rewriteCond %{REQUEST_URI} !(\.css$)
rewriteCond %{REQUEST_URI} !(/robots\.txt$)
rewriteCond %{REQUEST_URI} !(\.png$)
rewriteCond %{REQUEST_URI} !(\.jpg$)
rewriteCond %{REQUEST_URI} !(\.jpeg$)
rewriteCond %{REQUEST_URI} !(\.pdf$)
rewriteCond %{REQUEST_URI} !(\.gif$)
rewriteCond %{REQUEST_URI} !(\.GIF$)
rewriteCond %{REQUEST_URI} !(\.xml$)
rewriteCond %{REQUEST_URI} !(\.js$)
rewriteCond %{REQUEST_URI} !(\.ico$)
RewriteRule . pageDispatcher.php [L]
Change your code to this:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule (?!^admin/)^.*$ pageDispatcher.php [L]

htaccess load resources from directory

I want to make a htaccess RewriteRule with a RewriteCond
What I have so far:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(sql.site.ro)$
RewriteRule ^(.*)$ ro-site-sql/index.php?x=$1
I want all requests sent to sql.site.ro to be served from the /ro-site-sql directory.
So if the URI is sql.site.ro/index.php the server will go to /ro-site-sql/index.php, or if URI is sql.site.ro/images/small/9954.png the server will go to /ro-site-sql/images/small/9954.png.
The problem is that if I make
RewriteCond %{HTTP_HOST} ^(sql.site.ro)$
RewriteRule ^(.*)$ ro-site-sql/$1
I get an Internal Server Error, and if I make
RewriteCond %{HTTP_HOST} ^(sql.site.ro)$
RewriteRule ^(.*)$ ro-site-sql/index.php?x=$1
I get Array ( [x] => ro-site-sql/index.php ) inside of /ro-site-sql/index.php. Should it not be just index.php, without the ro-site-sql/ part?
Thanks!
RewriteCond %{HTTP_HOST} ^sql.site.ro$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 ^(\w+/)+\w+\.\w+$
RewriteRule ^(.*)$ ro-site-sql/$1 [L,QSA]
RewriteCond %{HTTP_HOST} ^sql.site.ro$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(\w+/)+\w+\.\w+$
RewriteRule ^(.*)$ ro-site-sql/index.php?x=$1 [L,QSA]
This should work to identify actual files and access those and pass anything else to index.php.
The rewrite engine loops over .htaccess files until no more substitutions occur. You need to ensure that this loop stops after the first replacement. There are various ways to do this. Perhaps the easiest to understand is:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(sql.site.ro)$
RewriteCond $1 !index.php$
RewriteRule ^(.*)$ ro-site-sql/index.php?x=$1 [L,QSA]
Add the [L] flag because this is the last rule on this cycle and [QSA] if you also have other parameters sometimes.

clashing htaccess rules .php file extention still accessible

I have been trying to get my urls re-written. The first 4 rules are vital, but they are clashing with this line: (i think).
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^.#?\ ]+)\.php([#?][^\ ]*)?\ HTTP/
this stops the url being able to be accessed like so www.example.com/page.php and redirects to www.example.com/page/
after adding the first 4 rules you can see in the htaccess, the above condition doesnt seem to work and Im able to access urls like this :(
lovelakedistrict.com/lake-district-cottages.php/cottages/5/
obviously i should be like this
lovelakedistrict.com/lake-district-cottages/cottages/5/
however this still works :)
lovelakedistrict.com/lake-district-cottages.php -->
lovelakedistrict.com/lake-district-cottages/
This is my htaccess file- can anyone see what happening?
Options +FollowSymlinks
Options +Indexes
RewriteEngine on
#these 4 rules stop being able to access pages like eg
#/lake-district-cottages/?cottages=5/
#/lake-district-cottages/?cottages/5/ --> all direct to /lake-district-cottages/cottages/5/
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /[^?\ ]*\?[^\ ]+
RewriteCond %{QUERY_STRING} ^/*([^=&]*[^=&/])/*(&+(.*))$
RewriteRule ^(.*[^/])/?$ /$1/%1/?%3 [N]
RewriteCond %{REQUEST_URI} !^/result
RewriteCond %{REQUEST_URI} !^/shop-result
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /[^?\ ]*\?[^\ ]+
RewriteCond %{QUERY_STRING} ^/*([^=&]*[^=&/])/*$
RewriteRule ^(.*[^/])/?$ /$1/%1/?%4 [L,R=301]
RewriteCond %{REQUEST_URI} !^/result
RewriteCond %{REQUEST_URI} !^/shop-result
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /[^?\ ]*\?[^\ ]+
RewriteCond %{QUERY_STRING} ^/*([^=&]*[^=&/])/*=/*([^&]*[^&/])/*(&+(.*))$
RewriteRule ^(.*[^/])/?$ /$1/%1/%2/?%4 [N]
RewriteCond %{REQUEST_URI} !^/result
RewriteCond %{REQUEST_URI} !^/shop-result
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /[^?\ ]*\?[^\ ]+
RewriteCond %{QUERY_STRING} ^/*([^=&]*[^=&/])/*=/*([^&]*[^&/])/*$
RewriteRule ^(.*[^/])/?$ /$1/%1/%2/? [L,R=301]
RewriteCond %{REQUEST_URI} !^/result
RewriteCond %{REQUEST_URI} !^/shop-result
# this stops you accessing pages with php extention eg
#/lake-district-cottages.php --> directs to /lake-district-cottages/
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^.#?\ ]+)\.php([#?][^\ ]*)?\ HTTP/
# this ignors the include folder and does not add trailing slash (so ajax file works)
RewriteCond %1 !^include/
RewriteRule ^([^.]+)\.php$ /$1 [R=301,L]
#this removes php extention
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
#this removes php extention and adds trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ $1.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
#these re-write urls allowing / to replace ? and =
RewriteRule ^lake-district-cottages/cottages/([0-9]+) lake-district-cottages.php?cottages=$1
RewriteRule ^lake-district-hotels/hotels/([0-9]+) lake-district-hotels.php?hotels=$1
RewriteRule ^lake-district-bed-and-breakfast/bed-and-breakfast/([0-9]+) lake-district-bed-and-breakfast.php?bed-and-breakfast=$1
RewriteRule ^lake-district-lodges/lodges/([0-9]+) lake-district-lodges.php?lodges=$1
RewriteRule ^lake-district-cottages-shop/lake-district-book-shop/([0-9]+) lake-district-cottages-shop.php?lake-district-book-shop=$1
RewriteRule ^lake-district-lodges/lodges/([0-9]+) lake-district-lodges.php?lodges=$1
RewriteRule ^result/([a-zA-Z0-9])/([0-9]+) result.php?$1=$2
RewriteRule ^shop-result/([a-zA-Z0-9])/([0-9]+) shop-result.php?$1=$2
The condition pattern doesn't match your URLs that contain PATH_INFO, but simplifying it a bit to something like this should take care of that:
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^.?\ ]+)\.php
RewriteCond %1 !^include/
RewriteRule ^([^.]+)\.php(/.+)?$ /$1%{PATH_INFO} [R=301,L]