Multi-Country Using .htacces - apache

I am trying to achieve any one of the following:
http://ca.domain.com/Category/SubCategory -> http://www.domain.com/index.php?country=ca&category=Category&subcategory=Subcategory
http://www.domain.com/ca/Category/SubCategory -> http://www.domain.com/index.php?country=ca&category=Category&subcategory=Subcategory
I have browsed around but unfortunately haven't been able to figure out any working solution. For the first kind I tried the following code:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com
RewriteRule ^(.*)/$ http://www.domain.com/index.php&country=%1&category=$1 [L]
The issue with sub domain is that it doesn't redirect anywhere at all. I read around and it says I need to setup wildcard virtual host. I haven't been able to figure out how to do that on godaddy. So not sure how to do it.
As for the second kind, I used the following code:
RewriteEngine on
RewriteRule ^(.*)/?(.*)/ home.php?country=$1&category=$2 [B]
But that didn't work for me either. Any help in this regard would be highly appreciated. Thank you
As for Category & Subcategory, Subcategory is kind of mandatory. So it should also work for following link:
http://www.domain.com/ca/Toronto/Programming -> http://www.domain.com/index.php?country=ca&location=Toronto&subcategory=Programming
http://www.domain.com/ca/Programming -> http://www.domain.com/index.php?country=ca&location=&subcategory=Programming
I am not quite sure as to how to tell .htaccess if it is $1=subcategory or $2=subcategory since they switch in scenario 1 & 2.
Thanks to immense help from #anubhava I have reached to the following:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteCond %{HTTP_HOST} ^((?!www\.)[^.]+)\.domain\.com$ [NC]
RewriteRule ^([^/]+)/?$ adlisting.php?country=%1&category=$1 [L,QSA]
RewriteCond %{HTTP_HOST} ^((?!www\.)[^.]+)\.domain\.com$ [NC]
RewriteRule ^([^/]+)/([^/]+)/?$ adlisting.php?country=%1&location=$1&category=$2 [L,QSA]
RewriteCond %{HTTP_HOST} ^(www\.)?\.domain\.com$ [NC]
RewriteRule ^([a-z]{2})/([^/]+)/?$ adlisting.php?country=$1&category=$2 [L,QSA,NC]
RewriteCond %{HTTP_HOST} ^(www\.)?\.domain\.com$ [NC]
RewriteRule ^([a-z]{2})/([^/]+)/([^/]+)/?$ adlisting.php?country=$1&category=$2&subcategory=$3 [L,QSA,NC]

Have these rules in root .htaccess:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteCond %{HTTP_HOST} ^((?!www\.)[^.]+)\.domain\.com$ [NC]
RewriteRule ^([^/]+)/?$ index.php?country=%1&category=$1 [L,QSA]
RewriteCond %{HTTP_HOST} ^((?!www\.)[^.]+)\.domain\.com$ [NC]
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?country=%1&category=$1&subcategory=$2 [L,QSA]
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^([a-z]{2})/([^/]+)/?$ index.php?country=$1&category=$2 [L,QSA,NC]
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^([a-z]{2})/([^/]+)/([^/]+)/?$ index.php?country=$1&category=$2&subcategory=$3 [L,QSA,NC]

Related

Apache2, mod_rewrite - use variable from prev condition in next condition

I would like to check if file exist in subdirectory.
And I mean subdirectory that "is taken" from "subdomain".
Eg. url: "test.domain.com/file.txt" file location is "/test/file.txt"
I have wildacrd and i can't hardcode "test" directory. How can i do it?
I tried add %{DOCUMENT_ROOT}%1/$1 but this dosen't work.
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain.com
RewriteCond %{DOCUMENT_ROOT}%1/$1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}%1/$1 -d
RewriteRule ^ /%1%{REQUEST_URI} [L]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain.com
RewriteRule ^images/(.*)$ /%1/image.php?test=$1 [L]
With your shown samples, please try following. Please make sure to clear your browser cache before testing your URLs.
Options +FollowSymLinks -MultiViews
RewriteEngine ON
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain.com [NC]
RewriteCond %{DOCUMENT_ROOT}/%1/$1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}/%1/$1 -d
RewriteRule ^(.*)/?$ %1/$1 [L]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain.com [NC]
RewriteRule ^images/(.*)/?$ %1/image.php?test=$1 [QSA,NC,L]
Fixes in OP's attempts: You need not to use condition RewriteCond %{ENV:REDIRECT_STATUS} ^$ in 2nd Rules sets, even first one may not need it but for safer side you can have it. Then your sequence of accessing path from %{DOCUMENT_ROOT} was not correct which was corrected here.

htaccess rewriting subdomain to a folder

I want to rewrite my main domain to a path and if no query is appended then to a different path. I have a wildcard C name in place to support subdomains.
www.xxx.com => works normally
abc.xxx.com => point to a folder
abc.xxx.com/abc-xyz => point to www.xxx.com and should work normally
This is what i have right now :
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_HOST} !^([w]{3,3}[.]{1,1}){0,1}example.com$
RewriteCond %{HTTP_HOST} ^([0-9a-zA-Z-]*)[.]example.com$
RewriteRule ^$ portfolio/index.php?pageDetailId=%1 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond %{HTTP_HOST} !^([w]{3,3}[.]{1,1}){0,1}example.com$
RewriteCond %{HTTP_HOST} ^([0-9a-zA-Z-]*)[.]example.com$
RewriteRule ^(.*)$ $1?pageDetailId=%1 [QSA,L]
</IfModule>
As we can see, i need that abc as variable in abc.example.com/abc-xyz. Please reply with doubts with any, i tried many ways its not working. This same htaccess works on 1 domain smoothly but when i created a new domain its not working on that. Thanks.
NOTE: It works on httpd but not on apache2
Have it like this:
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} ^(?!www\.)([\w-]+)\.example\.com$ [NC]
RewriteRule ^(index\.php)?$ portfolio/index.php?pageDetailId=%1 [NC,L,QSA]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteCond %{HTTP_HOST} ^(?!www\.)([\w-]+)\.example\.com$ [NC]
RewriteRule ^(.+)$ index.php?pageDetailId=%1&/$1 [L,QSA]
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]

.htaccess won't rewrite my url at all

I've been crawling forums for about 2 hours and still haven't found the solution to my problem so I am turning to you guys for help.
My URL looks like this
http://gymbirdz.com/article.php?url=benefits-of-fish-oil
I want it to look like this
http://gymbirdz.com/article/benefits-of-fish-oil
This is my .htaccess request but It does not work and still no luck fixing it :/
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?article/([A-Za-z0-9-]+)/?$ /article.php?url=$1 [NC,R=301,L]
Any help is appreciated very much! Thank You
UPDATE
This is how my .htaccess looks from above
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^article/([A-Za-z0-9\-]+)$ article.php?url=$1 [NC,R=301,L,QSA]
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteCond %{REQUEST_URI} !/system/.* [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
RewriteCond %{HTTP_HOST} ^www\.gymbirdz\.com$
RewriteRule ^/?$ "http\:\/\/gymbirdz\.com\/" [R=301,L]
RewriteCond %{HTTP_HOST} ^192\.254\.185\.208
RewriteRule (.*) http://www.gymbirdz.com/$1 [R=301,L]
RewriteCond %{HTTP_USER_AGENT} libwww-perl.*
RewriteRule .* ? [F,L]
Try this rule your site root .htaccess:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} libwww-perl [NC]
RewriteRule ^ - [F,L]
RewriteCond %{HTTP_HOST} ^www\.gymbirdz\.com$ [OR]
RewriteCond %{HTTP_HOST} ^192\.254\.185\.208$
RewriteRule (.*) http://gymbirdz.com/$1 [R=301,NE,L]
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteCond %{REQUEST_URI} !/system/.* [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
RewriteCond %{THE_REQUEST} /article\.php\?url=([^\s&]+) [NC]
RewriteRule ^ /article/%1? [R=302,L,NE]
RewriteRule ^/?article/([\w-]+)/?$ article.php?url=$1 [NC,QSA,L]
Maybe I don't understand your question, but this is from the documentation:
Syntax: RewriteRule Pattern Substitution [flags]
This is your rule:
RewriteRule ^/?article/([A-Za-z0-9-]+)/?$ /article.php?url=$1 [NC,R=301,L]
It appears that you may have the two arguments reversed. Try something like this instead:
RewriteRule ^article.php?url=([\w-]+) /article/$1 [NC,L,R=301,QSA]
This assumes that the .htaccess file is in the same directory as article.php.

.htaccess friendly url´s issue

I am implementing friendly URLs on my website.
At the moment I have some rules working but still need help to implement the last ones.
Rules working:
RewriteRule ^SobreNos$ /about.php [L]
RewriteRule ^Contactos$ /contact.php [L]
Rule Working but not as I wanted:
RewriteRule ^catalogo-([a-zA-Z0-9]+)$ catalogo.php?cat=$1
Rule not working:
RewriteRule
^products/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ product.php?id=$1&ref=$2&cat=$3 [L]
About the 3rd rule, I wanted the friendly URL catalogo/homem and tried to change the - for the / but didn't work.
The 4th rule don´t work at all, with - or the /.
Could be a problem of the server's version? This is the version of the server "Apache ver.2.2.19 (Unix)".
My .htaccess file
RewriteBase /
Options -Indexes
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteRule ^SobreNos$ /about.php [L]
RewriteRule ^Contactos$ /contact.php [L]
RewriteRule ^catalogo-([a-zA-Z0-9]+)$ catalogo.php?cat=$1
RewriteRule
^products/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ product.php?id=$1&ref=$2&cat=$3 [NC,L]
I have reformatted your file to the following:
Options -Indexes
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteRule ^SobreNos$ about.php [L]
RewriteRule ^Contactos$ contact.php [L]
RewriteRule ^catalogo/([a-z0-9]+)/?$ catalogo.php?cat=$1 [NC,L]
RewriteRule ^products/([a-z0-9-]+)/([a-z0-9-]+)/([a-z0-9-]+)/?$ product.php?id=$1&ref=$2&cat=$3 [NC,L]
Here's what I've changed:
Moved your RewriteBase down below RewriteEngin on
Removed the leading slashes in the destinations for each rule (they are not needed because RewriteBase is set)
Removed A-Z from your expressions (because the NC, no case, flag is set)
Used the slash in the 3rd and 4th rules, and appended an optional ending slash.
My test show that everything works properly. The only reason a 404 would be thrown is if the file you are rewriting to (product.php for example) does not exist.
I have the solution for the problem.
Options -Indexes
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ catalogo.php?cat=$1 [L,QSA]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/.]+)/([^/]+)$ catalogo.php?cat=$1&scat=$2 [L,QSA]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)$ product.php?cat=$1&scat=$2&name=$3&id=$4 [L,QSA]
Still have to add a rewrite rule for about.php and contactos.php, but not a huge problem.

getting htaccess rewrite rules to rewrite only specific urls

I only want to rewrite this specific url:
http://www.website.co.uk/php/project.php?project=anythingHere
to
http://www.website.co.uk/php/project/anythingHere
this works fine but when I go to http://www.website.co.uk/php/project/anythingHere
it has changed all the links as well (style sheets, javascript, hyperlinks... things I don't want to change). for example:
http://www.website.co.uk/php/index.php
to
http://www.website.co.uk/php/project/index.php
This is what I have already
RewriteEngine On
RewriteCond %{HTTP_HOST} ^website.com\.co\.uk$ [OR]
RewriteCond %{HTTP_HOST} ^www\.website.com\.co\.uk$
RewriteRule ^clients$ "http\:\/\/website.com\.co\.uk\/billing\/index\.php" [R=301]
RewriteRule ^project/([^/]*)$ /php/project.php?project=$1
Can you try this code:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?website\.co\.uk$ [NC]
RewriteRule ^clients/?$ http://website.co.uk/billing/index.php [NC,L,R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(php/project)\.php\?project=([^\s&]+) [NC]
RewriteRule ^ /%1/%2? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(php/project)/([^/]+)/?$ /$1.php?project=$2 [L,QSA,NC]