url rewriting in .htaccess - regex

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.

Related

.htaccess RewriteRule with two different conditions

I have two different URLs:
1.: mysite.com/file.php
2.: mysite.com/**articles**.php?article=**something**
I would like to rewrite mysite.com/file.php to mysite.com/file.
AND
I would like to rewrite mysite.com/**articles**.php?article=something to mysite.com/**news**/**something**
I tried this but it's not working.
It rewrite mysite.com/file.php to mysite.com/file, but does not rewrite mysite.com/**articles**.php?article=**something** to mysite.com/**news**/**something**
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^?\ ]+)\.php
RewriteCond %{REQUEST_FILENAME} !exception.php
RewriteRule ^news/([^/]+)(/*)$ /articles.php?article=$1
RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^/?(.*)$ /$1.php [L]
(There is one exception in the code: exception.php which I don't want to rewrite.)
Can you please correct my code? I've been working on this for 4 days, I've read everything, but I can't do it.
With your shown samples and attempts please try following .htaccess rules file. Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
##External redirect for file.php file.
RewriteCond %{REQUEST_URI} !/?exception\.php [NC]
RewriteCond %{THE_REQUEST} \s/(file)\.php/?\s [NC]
RewriteRule ^ /%1? [R=301,L]
##Internal rewrite for php files.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/?$ $1.php [QSA,L]
##external + internal rewrite rules for articile.php here.
RewriteCond %{THE_REQUEST} \s/articles\.php\?article=(\S+)\s [NC]
RewriteRule ^ /news/%1? [R=301,L]
RewriteRule ^news/([^/]*)$ articles.php?article=$1 [NC,QSA,L]

rewrite query sign in url

I have url with query in the midle of request uri ex:
http://domain.com/index.php?/recipe_category/recipe/
I've trying to remove the index.php as well but it leave the query sign (?) so the url become
http://domain.com/?/recipe_category/recipe/
How to remove all of the [index.php?]? For your information, my htaccess written as below :
RewriteEngine On
RewriteCond %{SERVER_NAME} !^www.domain.com$
RewriteRule .* http://www.domain.com%{REQUEST_URI} [R=301,L]
RewriteRule ^index.php/(.*)$ http://www.domain.com/$1 [R=302,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php
RewriteRule ^index\.php$ / [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|robots\.txt|sitemap\.xml)
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
You can use following lines to remove "index.php?"
RewriteEngine On
RewriteRule ^index.php\?(.*)$ http://www.domain.com/$1 [R=302,L]

Removing GET parameter in .htaccess

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.

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]