Automatically redirect to sub-domain with htaccess - php

I am building a web application in which user is assigned a sub-domain,
Now somehow I am able write an .htaccess code which allow me to get sub-domain and also retrive the content from the database.
.htaccess code :
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^([w]{3,3}[.]{1,1}){0,1}kanai.in$
RewriteCond %{HTTP_HOST} ^([0-9a-zA-Z-]*)[.]kanai.in$
RewriteRule ^$ website.php?url=%1 [NC,L]
orginal link is kanai.in/website.php?url=test
and when I write test.kanai.in it is successfully giving the content which is stored in database,
What I want is that kanai.in/website.php?url=test should automatically redirect to test.kanai.in

You can try this code :
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} url=(.*)
RewriteRule ^website.php$ http://%1.kanai.in? [L,R=301]
We search for url value in query string.
We check that we are on website.php page.
We redirect to the value subdomain and remove query parameters by adding a ?
Those links might help you :
Apache wiki page on how to manipulate the Query String
Test your apache htaccess files online

Guess what, I got the answer,
My htaccess code is correct,
All I have to do is just give it a href link as "http://test3.kanai.in",
So it will redirect to test3.kanai.in and will get the corresponding data from the database.....

Related

Rewrite URL containing ... in htaccess

Sometimes I find that URLs on my website show up with mywebsite_smf_smf1_session=.
For example:
http://mywebsite.com/index.php​?mywebsite_smf_smf1_session=6goevo28​vf18ubqgb5uh6r08b2&topic=332242​.msg916981
http://mywebsite.com/index.php​?mywebsite_smf_smf1_session=6gqvo88l​u5fnl1qmj7temt0113&topic=316196​.msg931242
The part that contains mywebsite_smf_smf1_session=### shouldn't be in the URL, but the link still redirects to the correct page.
I would like to know how I can use mod_rewrite in .htaccess so that when someone clicks a URL containing mywebsite_smf_smf1_session=###&, it automatically reverts to the version not containing it:
Clicking:
http://
mywebsite.com/index.php​?mywebsite_smf_smf1_session=6gqvo88l​u5fnl1qmj7temt0113&topic=316196​.msg931242
Becomes:
http:// mywebsite.com/index.php​?topic=316196​.msg931242
You can use this rule in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^mywebsite_smf_smf1_session=[^&]*(?:&(.*))?$ [NC]
RewriteRule ^index\.php$ %{REQUEST_URI}?%1 [NE,L,NC,R=302]

mod_rewrite with name replacing ID

I have a page on my website that dynamically displays content. The URL structure is mywebsite.com/giveaway/giveaway.php?id=(any number)
I wish to change that dynamic URL into a static/friendly URL mywebsite.com/giveaway/name-of-giveaway-corresponding-to-id.
In my .htaccess file found in my root folder i have the following:
RewriteEngine On
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+\?page=([^\s&]+) [NC]
RewriteRule ^ /page/%1? [R=301,L]
# existing rule
RewriteRule ^page/([^/]+)/?$ /?page=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [L]
The current .htaccess file removes .php and also redirects and changes a URL on my site from
mywebsite.com?page=number to mywebsite.com/page/number
I know you have to get the name corresponding to the ID in php from my database and use that, but im not sure how to write the code in the .htaccess file or how to use and pass the results from the database. Thanks for any help!
I don't know if that is what you mean, but you can't connect the rewrite engine, which is part of the underlying Apache server layer, to the database, which has to be accessed by php.
So you can't rewrite the "name-of-giveaway-corresponding-to-id" to the id directly, you need to rewrite it to something like giveaway.php?nameofgiveaway=(name of giveaway) and then search the database for that string.
Your way to go is to add a rewrite rule like
RewriteRule ^giveaway/([^/]+)$ giveaway.php?nameofgiveaway=$1 [L,QSA]
(not tested, so forgive me if something is wrong) and search for $_GET['nameofgiveaway'].

Redirect direct image urls requests and pass the original url value in the redirection

Suppose I have someone (or Google Images) trying to access
http://null.com/uploads/someimage.jpg (actually any image in /uploads folder)
I'd want htaccess to redirect the user to
http://null.com/page/
HOWEVER, I need to pass the original url (http://null.com/uploads/someimage.jpg) to the redirection target page as a POST string value which the target php page can use to do stuff with it
Would it be possible to achieve that with htaccess?
UPDATE - and also, it would be interesting to have this working only when the user agent is a human operated browser, not a bot such as google bot, in this way Google and other search engines can crawl images appropriately without the redirection
You may try this:
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !page\.php [NC]
RewriteCond %{REQUEST_URI} ^/uploads/([^/]+)/? [NC]
RewriteRule . page.php?url=uploads/%1 [L]
Maps silently
http://null.com/uploads/someimage.jpg with or without trailing slash
To:
http://null.com/page.php?url=uploads/someimage.jpg
The string uploads is assumed to be fixed, while someimage.jpg can be any name.
The script name page.php is an example an can be any name also. Replace all instances in the rule-set.
For permanent and visible redirection, replace [L] with [R=301,L]
This answer is according to this description in OP comments:
"...redirect the user to this page and pass the former original url as a post value..."
where page is a script.
NOTE: The parameters passed in the substitution URL can be captured with PHP using $_GET or $_REQUEST, not $_POST.
UPDATE
Here is another version:
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !page\.php [NC]
RewriteRule ^(.*)\.jpg page.php?url=$1.jpg [L]
Maps silently
http://null.com/any/number/of/folders/someimage.jpg
To:
http://null.com/page.php?url=any/number/of/folders/someimage.jpg
The rewrite rule is applied only when the incoming URL holds a file with extension jpg at the end of the path.

Rewrite url which redirects to page on webserver

I am trying to get a page with a query string to redirect to a nicer looking url then get that url and transfer it back to the original query string but without redirecting (i.e. without changing the url)
At the moment I am getting a redirect loop (for obvious reasons) but I was hoping for a way to stop this.
This is my code in my htaccess file
#rewrite search querystring
#/search/'apartment'/2_bedrooms/price_0-500000/town_W4/development_18,SW5/
RewriteRule ^search/([^/]+)/([^/]+)_bedrooms/price_([^/]+)-([^/]+)/town_([^/]+)/development_([^/]+) /search.php?propertytype=$1&bedrooms=$2&minprice=$3&maxprice=$4&location=$5&development=$6 [NC]
RewriteCond %{QUERY_STRING} propertytype=([^/]+)&bedrooms=([^/]+)&minprice=([^/]+)&maxprice=([^/]+)&location=([^/]+)&development=([^/]+)
/search/$1/$2_bedrooms/price_$3-$4/town_$5/development_$6 [R,L]
RewriteRule ^(.*)$ /search/%1/%2_bedrooms/price_%3-%4/town_%5/development_%6? [R,L]
So what it is meant to do is:
user has been taken to:
http://www.domain.com/search/?propertytype=dev&bedrooms=2&minprice=0&maxprice=10000000&location=W1&development=W1
This page is the actual page on the server where the data is coming from, however I want the user to see.
http://www.domain.com/search/dev/2_bedrooms/price_0-10000000/town_W1/development_W1/
Is it possible to do this without a redirect loop.
Thanks for your help
EDIT I'm thinking it could be done with the rewrite flags but I'm not sure, I'm quite new to the Rewrite Engine
Edited:
Here is a complete (and working) solution for you:
RewriteEngine On
# User gets here:
# http://localhost/search/?propertytype=dev&bedrooms=2&minprice=0&maxprice=10000000&location=W1&development=W1
# He is explicit redirected to here:
# http://localhost/search/dev/2_bedrooms/price_0-10000000/town_W1/development_W1/
# Internally, apache calls this:
# http://localhost/search.php?propertytype=dev&bedrooms=2&minprice=0&maxprice=10000000&location=W1&development=W1
RewriteRule ^search/([^/]+)/([^/]+)_bedrooms/price_([^/]+)-([^/]+)/town_([^/]+)/development_([^/]+) search.php?propertytype=$1&bedrooms=$2&minprice=$3&maxprice=$4&location=$5&development=$6 [NC,PT]
RewriteCond %{QUERY_STRING} propertytype=([^/]+)&bedrooms=([^/]+)&minprice=([^/]+)&maxprice=([^/]+)&location=([^/]+)&development=([^/]+)
RewriteRule ^search/(.*)$ /search/%1/%2_bedrooms/price_%3-%4/town_%5/development_%6/? [R,L]
It assumes you put .htaccess in server root and that there is a file search.php in root too.
Original:
I think you can use PT and QSA Rewrite Rule flags (http://httpd.apache.org/docs/current/rewrite/flags.html) in your first rule
Use PT for server-side redirection (it will not change the URL for the user/browser, but will for your server-side scripts)
Use QSA if you wanna carry the query while doing this redirection
You can redirect all requests that don't target an existing file to a specific php-script, for example:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [PT,QSA]

How to Mask URL with Variables (.htaccess)

I have viewed similar questions to mine, but it does not pertain to my specific example.
1st URL (what is to be sent to the server and is originally inputted to the URL bar): www.site.com/?variable1=3&variable2=filename.php
2nd URL (what is shown to the user in the URL bar): www.site.com/filename.php
where "filename.php" is the same value as "variable2" in the original URL.
I have seen examples of the opposite, where someone enters the 2nd URL and it replaces it with the 1st URL, but that's not what I want.
Currently what I have:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^/([A-Za-z0-9]+)$ /?CommID=$1&FileName=$2 [NC,L]
which I got the structure from the link provided from this question, but that's not working.
I'm not trying to redirect, just mask the current URL so it is SEO friendly. I need to grab both variables from the URL to have the dynamic content be pulled correctly.
Any ideas?
Try adding the following to your .htaccess file in the root of your domain
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
#if the query string has a commid and a fIlename Param
RewriteCond %{QUERY_STRING} ^CommID=([^&]+)&FileName=([^&]+) [NC]
#redirect users to the Pretty URL of www.site.com/variable1/filename.php
RewriteRule .* /%1/%2? [L,R=301]
#for a URL like this in address bar www.site.com/variable1/filename.php
#CommID will contain variable1 and FileName will contain filename.php
RewriteRule ^([A-Za-z0-9]+)/([A-Za-z0-9\.]+)$ /?CommID=$1&FileName=$2 [NC,L]
Edit:
Also redirect users using the query string URL to the pretty URL

Categories