Basic redirect with htaccess - .htaccess

I want to redirect the url of sample.mysite.com to
mysite.com/shops/index/sample.mysite.com . How can i do this using
.htaccess rules ?
Note : Here sample can be a variable ..

You can use the following rewriteRule
RewriteEngine on
RewriteCond %{HTTP_HOST} ^sample\.mysite\.com$
RewriteRule ^ /shops/index/sample.mysite.com%{REQUEST_URI} [L,R,NE]
This will internally forward sample.mysite.com to mysite.com/shops/index/sample.mysite.com with the requested uri.

Related

I can't apply two rules at the same time in htaccess

and thanks to read me.
My goal is to have the same htaccess code in local and production.
First, I need to rewrite example.com/index.php?action=somepage to example.com/somepage.
Second, I must rewrite http://example.com to https://example.com, but only in production, not on localhost.
So far this is my code:
RewriteEngine on
RewriteBase /
# For Ionos
RewriteRule ^([0-9a-zA-Z/\ -]+)(?:&([0-9a-zA-Z&=_\ -]+))?$ index.php?action=$1&$2
# $1 : route name and framework parameters
# $2 : classic $_GET parameters (&param=value)
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,NE,R=301]
When I use https:// to connect, no problem, the first rule concerning index and action apply.
When I use http://, the adress become https://, BUT the index/action rule doesn't apply.
Thanks a lot!
Edit : this works :
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,R=301]
# For Ionos, http => https
RewriteRule ^([0-9a-zA-Z/\ -]+)(?:&([0-9a-zA-Z&=_\ -]+))?$ index.php?action=$1&$2
# $1 : route name and framework parameters
# $2 : classic $_GET parameters (&param=value)
But I have a last problem.
My folder in like this:
[] example
...[]public
......htaccess
...htaccess
The code shown above is the htaccess of public directory. The htaccess of example directory redirect to public:
RewriteEngine on
RewriteBase /
# for Ionos
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]
The problem is while http://example.com redirect to https://example.com, http://example.com/somepage redirect to https://example.com/public/somepage and I can't remove the "public" part.
Thanks!
Your two rules "were" in the wrong order. Your external redirect (HTTP to HTTPS) needs to be before the internal rewrite. Your first rule (rewrite) would have still applied, but it would have resulted in an external redirect to index.php?action=... (exposing your internal file/URL structure).
However, you are also missing L flags on both these rules.
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,R=301]
If this is in the /public/.htaccess file and the /public subdirectory is hidden (ie. it's being internally rewritten to) then you need to change the RewriteRule to read:
RewriteRule (.*) https://%{HTTP_HOST}/$1 [R=301,L]
After the request is internally rewritten by the parent config, the REQUEST_URI server variable contains the full URL-path, including the "hidden" /public subdirectory, so if you redirect to REQUEST_URI it will expose the hidden subdirectory. Whereas, if you use a backreference to the matched RewriteRule pattern, which matches against a URL-path that is relative to the current directory then this naturally excludes the /public subdirectory.

How to redirect this URL using htaccess?

I want to redirect following URL
http://www.example.com/friends/view.php?l=ka&id=21903
Into this page
http://www.example.com/hello.html
How can I do this using htaccess?
You can use the following rule in htaccess :
RewriteEngine on
RewriteCond %{THE_REQUEST} \s/friends/view\.php\?l=ka&id=21903\s [NC]
RewriteRule ^ http://example.com/hello.html? [L,R]

htaccess redirect to url parameter

Is it possible to redirect with htaccess, from
http://example.com/redirect.php?url=http%3A%2F%2Fanother.com%2Fsmiley.gif
to url parameter,
http://another.com/smiley.gif
Please advice.
You can use this :
RewriteEngine on
RewriteCond %{THE_REQUEST} /redirect\.php\?url=.+([^/]+\.gif)\s [NC]
RewriteRule ^ http://another.com/%1? [NE,L,R]
This will redirect /redirect.php?url=foobar/foobar.gif to http://another.com/foobar.gif .

URL Masking with htaccess

I have a url below:
http://www.mysite.com/mypage.php?PropertyBuyRent=rent&Developer=&Resort=&City=&State=&Country=&Price=
if PropertyBuyRent=rent and all other variables are empty then url should be like:
http://www.mysite.com/mypage.php/TimeshareForRent/All
how can i write htaccess for this?
You can use this rule:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(testing/mypage\.php)\?PropertyBuyRent=rent&Developer=&Resort=&City=&State=&Country=&Price= [NC]
RewriteRule ^ /%1/TimeshareForRent/All? [R=301,L]
RewriteRule ^(testing/mypage.php)/TimeshareForRent/All/?$ /$1?PropertyBuyRent=rent&Developer=&Resort=&City=&State=&Country=&Price= [L,NC,QSA]
Reference: Apache mod_rewrite Introduction
You can use this line and make edits in php file as follows
RewriteRule ^mypage.php/TimeshareForRent/All/?$ mypage.php?PropertyBuyRent=rent&Developer=&Resort=&City=&State=&Country=&Price= [L,NC,QSA]
You can tell your php file to redirect to "mypage.php/TimeshareForRent/All" when your condition matched, because with above rule, it will mask the url of variables with the one without variable

Rewrite rule .htaccess from blog.domain.com/anypath to www.domain.com/blog/anypath

I'm trying to write a rule in my .htaccess file so that any visitors from blog.domain.com/anypath are redirected to www.domain.com/blog/anypath
The rule that I've written below only seem to redirect blog.domain.com to domain.com/blog but doesn't seem to redirect correctly if the URL contains a path such as blog.domain.com/path
RewriteCond %{HTTP_HOST} ^blog\.* [NC]
RewriteRule .* http://www.domain.com/blog [L]
Any help is appreciated.
This is because you have not captured the path with a () capture group to rewrite via the variable $1. You will need to append that to your redirection URL.
RewriteCond %{HTTP_HOST} ^blog\.* [NC]
# Capture the full path into $1 and append it to the output URL
RewriteRule (.*) http://www.domain.com/blog/$1 [L,R=301]
If this is to be a permanent redirection, you should use R=301 in [L,R=301]
As always, consult the mod_rewrite documentation for full details, and use this clever rewrite tester for experimentation.
You'll need to capture your REQUEST_URI and pass it onto the path.
RewriteCond %{HTTP_HOST} ^blog.domain.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/blog/$1 [R=301,L]
This will capture the path from the blog's subdomain, and redirect it to the correct URL.
For example, blog.domain.com/post/736/test will redirect to www.domain.com/blog/post/736/test.

Resources