.htaccess redirects to subdomain with %{REQUEST_FILENAME} - .htaccess

We have an online shopping cart that was installed into root - we then decided that we wanted to install a CMS ito root and move the shopping cart to a subdomain.
So we have
domain.com
shop.domain.com
What we are trying to achieve is to redirect URLs that are as follows:
domain.com/product_info.php?products_id=X
To:
shop.domain.com/product_info.php?products_id=X
Where the value of X needs to change as well.
I read (and if I understand this correctly) it would have something to do with %{REQUEST_FILENAME} and so far we have this in our .htaccess, which was a wildcard redirect which is actually redirecting everything to the same file name but we want the value of X to change too.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain\.co.uk$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain\.co.uk$
RewriteRule ^product_info\.php\/?(.*)$ "http\:\/\/sub.domain\.co.uk\/product_info\.php\?products_id\=1$1" [R=301,L]

The QSA flag will automatically pass the query string back, put this .htaccess on the root folder of your domain:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^product_info\.php$ http://shop.domain.co.uk/product_info.php [R=301,QSA,L]
If the subdomain is also on the same root folder of your domain, then use this:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.co\.uk$ [NC]
RewriteRule ^product_info\.php$ http://shop.domain.co.uk/product_info.php [R=301,QSA,L]
So basically with any of the above rules, if the user access:
domain.com/product_info.php?products_id=4
domain.com/product_info.php?products_id=3
domain.com/product_info.php?products_id=2
domain.com/product_info.php?products_id=1
It will be redirected to:
shop.domain.com/product_info.php?products_id=4
shop.domain.com/product_info.php?products_id=3
shop.domain.com/product_info.php?products_id=2
shop.domain.com/product_info.php?products_id=1
If you in fact need to change the ID this is how you would do it:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} products_id=([^&]+) [NC]
RewriteRule ^product_info\.php$ http://shop.domain.co.uk/product_info.php?products_id=1%1 [R=301,L]
And if the domain and sub domain are on the same root folder this is how it would look like:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.co\.uk$ [NC]
RewriteCond %{QUERY_STRING} products_id=([^&]+) [NC]
RewriteRule ^product_info\.php$ http://shop.domain.co.uk/product_info.php?products_id=1%1 [R=301,L]
Basically you need to use %{QUERY_STRING} to get data from the query string.
So with any of the 2 above rules, if the user access:
domain.com/product_info.php?products_id=4
domain.com/product_info.php?products_id=3
domain.com/product_info.php?products_id=2
domain.com/product_info.php?products_id=1
It will be redirected to:
shop.domain.com/product_info.php?products_id=14
shop.domain.com/product_info.php?products_id=13
shop.domain.com/product_info.php?products_id=12
shop.domain.com/product_info.php?products_id=11

Related

htaccess redirect subdomain into folder that has similiar name with subdomain

I did many way to do this, but always getting 404 error.
I want to redirect subdomain
[username].domain.com/
to
domain.com/user/[username]/index.php
and the url on browser address is not changed still
[username].domain.com/
[username] is dynamic according to registered user in my site.
my last trial httaccess setting is
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(^.*)\.domain\.com$
RewriteRule ^ ^.domain.com/user%{REQUEST_URI} [L,P]
Note: I did set my DNS wildcard *.domain.com
Thanks in advance,
Nanang K
============================================================
SOLVED
I had to create 3 htaccess files.
in root directory (redirect to user)
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(^.*)\.domain.com
RewriteRule ^(.*)$ /user/$1 [NC,L]
in user directory (redirect to username_folder)
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^([aA-zZ])$ user/$1/index.php
RewriteCond %{HTTP_HOST} ^(^.*)\.domain.com
RewriteRule (.*) user/%1/index.php
in username_folder directory (rewrite the url into original url)
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^([aA-zZ])$ index.php?siteName=$1
RewriteCond %{HTTP_HOST} ^(^.*)\.domain.com
RewriteRule (.*) index.php?siteName=%1
now, when user access robert.domain.com, he will get the content from domain.com/user/robert/index.php. And the url browser still robert.domain.com
hope helps someone who get similiar problem.
Hello Can you please try this :-
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$ [NC]
RewriteRule ^(.*)$ /users/$1 [L,NC]
It may use helpful.
It should be like this, try it out.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^[username].domain.com$
RewriteRule ^/?$ "http\:\/\/domain\.com\/users\/" [R=301,L]
hope this helps!

Redirection .htaccess depending of referer

I have a page that is www.example.com/page.html with a link on it that goes to on a blank page www.example.com/folder/link.php?id=123456
For a specific reason, when I'm on the same page but in other language (page will be like www.example.com/page_ro.html) I cannot change this link and need to use rewrite condition to do change that same link.
Let say I need the link to become www.example.com/folder/link.php?id=78910 in this case.
How can I do that in this case ? I've tried this with no success :
RewriteEngine On
Options +FollowSymLinks
RewriteCond %{HTTP_REFERER} .
RewriteCond %{HTTP_REFERER} ^http://www\.example\.com/page_ro.html [NC]
RewriteCond %(REQUEST_URI) folder/link\.php?id=123456$ [NC]
RewriteRule ^(.*) http://www.example.com/link.php?id=78910 [L]
You cannot match query string using REQUEST_URI variable. Use QUERY_STRING instead like this inside /folder/.htaccess:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /folder/
RewriteCond %{HTTP_REFERER} ^http://www\.example\.com/page_ro.html [NC]
RewriteCond %(QUERY_STRING) ^id=123456 [NC]
RewriteRule ^(link\.php)$ $1?id=78910 [NC,L,R=302]

htaccess redirect subfolder

I would like to redirect the url http://intranet/trac/paradox/report/6 to http://cobra.woking/trac/paradox/report/6. trac is a subfolder and paradox is a subfolder. report/6 are params that need to be kept and may change.
In my apache doc root i have
#/opt/html/.htaccess
Redirect 301 / http://intranet/intranet
RewriteEngine On
RewriteRule ^trac/paradox/report/6$ http://cobra.woking/trac/paradox/report/6 [L,R]
I have tried the following which does not work
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} !^intranet$
RewriteRule (.*) http://cobra.woking/$1 [R=301,L]
RewriteCond %{HTTP_HOST} !^intranet$
RewriteRule (.*) http://cobra.woking/$1 [R=301,L]
URL i want to change is http://intranet/trac/paradox/ to http://cobra.woking/trac/paradox/. I have placed .htaccess in the /opt/html/trac/paradox/.htaccess
In the htaccess file of your intranet's document root, add this to the top of the file:
RewriteEngine On
RewriteRule ^trac/paradox/report/6$ http://cobra.woking/trac/paradox/report/6 [L,R]

htaccess mod_rewrite .tld to specific folder

I want to do the following, if possible, with htaccess.
The webpage works this way, you get the index file via domain.de/de/start and the english version via domain.de/en/start.
If a user visits domain.com I would like him to end up at domain.de/en/ instead of domain.de/de/ so I need to rewrite all requests to domain.com but NOT requests with domain.com/xx/something to domain.de/en/
Thanks.
If I understand you clearly you only want to redirect http://domain.com/ to http://domain.de/en/ but do NOT want to redirect http://domain.com/XX/YY
Put this code in your .htaccess:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain\.com$
RewriteRule ^$ http://domain.de/en/ [L,R=301]
Try to use HTTP_HOST and rewritecond
RewriteCond %{HTTP_HOST}=domain.de
RewriteCond %{REQUEST_URI}=/ % redirect only the root
RewriteRule ^/$ /de/start [L] % to /de/start
RewriteCond %{HTTP_HOST}=domain.de
RewriteCond !%{REQUEST_URI}=/ % no root
RewriteCond !%{REQUEST_URI}=/[a-z]{2}/.* % except /xx/ directories
RewriteRule ^/.*$ /de/$1 [L] % push the user in the /de/ directory

.htaccess: Redirect root url to subdirectory, but keep root URL structure

I have created a site in a subdirectory and would like the site to appear as if it's in the root.
I used the below Mod_Rewrite code to get the site root to redirect to the subdirectory, but I would like the folder the files are held in to not appear.
currently: www.example.com/sitefiles/content/
Would like: www.example.com/content
Thanks
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteBase /
RewriteRule ^$ /sitefiles/ [L]
RewriteRule (.*) /sitefiles/$1 [L]
If it needs to be done via .htaccess and mod_rewrite, then use this:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/sitefiles/
RewriteRule (.*) /sitefiles/$1 [L]
Since you do not explicitly specify the page when website root will be hit, then there is no need for this line: RewriteRule ^$ /sitefiles/ [L]
You need to add condition for your main rewrite rule to prevent rewrite loop (when already rewritten URL gets rewritten again): RewriteCond %{REQUEST_URI} !^/sitefiles/
Well, the directory should have no reason to appear in any listing, and that mod_rewrite you posted should allow you to do as you said. Still, I would go about it a different way:
Options +FollowSymLinks +Indexes
RewriteEngine on
RewriteBase /
RewriteRule ^$ sitefiles/ [L]
RewriteRule ^(.+)$ sitefiles/$1 [L]
Hope this works.

Resources