htaccess Redirect BEFORE RewriteRule? - .htaccess

This is my htaccess:
## Rewrites
<IfModule mod_rewrite.c>
RewriteEngine On
Redirect /stream/ http://twitch.tv/8wayrun
Redirect /stream http://twitch.tv/8wayrun
RewriteCond %{HTTP_HOST} ^(www\.)?8wayrun\.com$
RewriteRule ^(.*)$ http://8wayrun.com/calibur/$1 [R=302,L]
</IfModule>
Basically, I need to to rewrite 8wayrun.com/stream to twitch.tv/8wayrun...
And then I need it to rewrite 8wayrun.com to 8wayrun.com/calibur...
The problem is, its rewriting 8wayrun.com/stream to 8wayrun.com/calibur/stream. How do I fix this?

The Redirect directive is part of mod_alias, and the Rewrite* directives are part of mod_rewrite. When a URI gets processed through the URL/file mapping pipeline, both modules get applied so having one in front of the other doesn't matter, both will get applied in the end.
You're better off sticking with only mod_rewrite and using the L flag to prevent the extra redirects from geting applied.
## Rewrites
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^/?stream/? http://twitch.tv/8wayrun [R=302,L]
RewriteCond %{HTTP_HOST} ^(www\.)?8wayrun\.com$
RewriteRule ^(.*)$ http://8wayrun.com/calibur/$1 [R=302,L]
</IfModule>

Related

How to redirect from /senturia-nam-sai-gon/?gallery=170 to /senturia-nam-sai-gon/projects/nha-pho-thuong-mai-5mx12m/?

I want to redirect https://senturia.com.vn/senturia-nam-sai-gon/?gallery=170
to https://senturia.com.vn/senturia-nam-sai-gon/projects/nha-pho-thuong-mai-5mx12m/
I tried this .htaccess file
<IfModule mod_rewrite.c>
RewriteCond %{QUERY_STRING} gallery=170 [NC]
RewriteRule (.*) /senturia-nam-sai-gon/project/nha-pho-thuong-mai-5mx12m/? [R=301,L]
</IfModule>
But it's not work
Your configuration looks fine. I highly recommend making sure that mod_rewrite is enabled on the server. Also, make sure that the .htaccess file is located in the correct place. If your are using some CMS with a single index.php file, the .htaccess should sit on the same level as that file. If senturia-nam-sai-gon is an actual directory on your server with its own index.php file, then the .htaccess should sit in that folder.
Here is how I would do it in a CMS:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{QUERY_STRING} (?:^|&)gallery=170 [NC]
RewriteRule ^senturia-nam-sai-gon/ /senturia-nam-sai-gon/project/nha-pho-thuong-mai-5mx12m/? [R=301,L,NC]
</IfModule>
Detailed break-down:
RewriteEngine on simply enables URL rewriting
RewriteBase / sets the base of all URLs (in this case simply /)
RewriteCond %{QUERY_STRING (?:^|&)galler=170 [NC] applies the following rule, if the GET parameters contain gallery=170. The [NC] flag makes the condition ignore upper- and lower-case
RewriteRule ^senturia-nam-sai-gon/ /senturia-nam-sai-gon/project/nha-pho-thuong-mai-5mx12m/? [R=301,L,NC] redirects /senturia-nam-sai-gon/ to /senturia-nam-sai-gon/project/nha-pho-thuong-mai-5mx12m/ with a status-code of 301 ("Moved permanently"). The ? at the end of the rule removes the querystring entirely. The L flag makes sure this is the last rule that gets applied. The NC flag does the same as before.

mod_rewrite keep rewriting recursilvely

I just want to append the url with home but it is happening in recursion and i get the browser saying too many redirects
<IfModule mod_rewrite.c>
RewriteEngine On
#RewriteRule ^test main.html
RewriteRule rest/(.*)$ /home/rest/$1 [R=301]
</IfModule>
so i want
localhost/rest/abc.php
to
localhost/home/rest/abc.php
Remove the slash before home.
RewriteRule ^rest/(.*)$ home/rest/$1 [R=301]

Can you continue to match htaccess patterns after 301 redirect?

My .htaccess file in my public_html has:
RewriteEngine on
RewriteRule ^testfolder/(.*)$ /$1 [R=301,L,NC]
RewriteRule ^(.*)$ testfolder/$1
According to http://htaccess.madewithlove.be/ which I am using to test output
when I enter http://www.mydomain.com/testfolder/aaa/bbb I get:
This rule was met, the new url is http://www.mydomain.com/aaa/bbb
Test are stopped, because of the R in your RewriteRule options. A redirect will be made with status code 301
Is there a way to perform the 301 redirect to make the url in the browser bar 'prettier'
while continuing to attempt to match patterns further down in the htacess file?
I have my codeigniter project in www.mydomain.com/testfolder. My first rule:
RewriteRule ^testfolder/(.*)$ /$1 [R=301,L,NC]
is to rewrite any navbar requests for a contoller which come in the form : www.mydomain.com/testfolder/controller/method to
www.mydomain.com/testfolder/controller/method. The second ruleis so that anyone www.mydomain.com/controller/method will use it turned internally into www.mydomain.com/testfolder/controller/method
Thank you in advance,
Bill
Addendum - my current .htaccess file:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+b1/(.*)\s [NC]
RewriteRule ^ /$1 [R=302,L]
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteRule ^(.*)$ /b1/$1 [L]
First of all your rules are flawed and will definitely cause infinite loop.
Reason being that 1st rule is externally redirecting /testfolder/aaa/bbb to /aaa/bbb.
Now after redirect 2nd rule will also kick in since you're just matching .* there and that will be internally forwarded to /testfolder/aaa/bbb/ and that rule will cause 1st rule to kick in once again and this will cause infinite loop.
Don't know your intent but you can use this code to avoid looping:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+b1/([^\s]+)\s [NC]
RewriteRule ^ /$1 [R=302,L]
RewriteRule ^(.*)$ /b1/$1 [L,P]
Once you verify it is working fine, replace R=302 to R=301. Avoid using R=301 (Permanent Redirect) while testing your mod_rewrite rules.
No. Once the client has been redirected a new request occurs and rewriting begins anew with the new URL.

Rewrite all files from folder to another folder

I want redirect all requested files from folder-a to folder-b. E.g. http://www.yoursite.com/folder-a/index.php to http://www.yoursite.com/folder-b/index.php.
How can I do that? I have verified if mod_rewrite works with this code:
RewriteEngine On
RewriteRule ^google.html$ http://www.google.com/ [R=301]
The structure on the webspace is the following:
.htacces
|-folder-a
|-folder-b
But if I want my folder redirect
RewriteEngine On
RewriteRule ^/folder-a/(.*)$ http://www.yoursite.com/folder-b/$1 [L,R=301]
the redirect doesn't work if I input the following URL:
http://www.yoursite.com/folder-a/
http://www.yoursite.com/folder-a/index.php
The redirect doesn't take place. I stay on the same page ... What I'm doing wrong? I also tried it with this htaccess:
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^folder-a(.*)$ http://www.yoursite.com/folder-b$1 [L,R=301]
</IfModule>
If all this works I want to exclude some files. Eg. with this htaccess:
RedirectPermanent /folder-a/info.php /folder-b/new-info.php
Edit:
Now I tried this htaccess
redirectMatch 301 ^/folder-a/ http://www.yoursite.com/folder-b
This works, but I need something which takes the whole path and rewrite it to the new folder.
This for example doesn't work:
RewriteRule ^folder-a/(.*)$ folder-b/$1
Solution
I had an old htacess file in my folder-a so the redirect didn't worked. This is my final htaccess:
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^folder-a/excludefile1.php http://www.yoursite.com/folder-b/newnameforfile1.php [L,R=301]
RewriteRule ^folder-a/excludefile2.php http://www.yoursite.com/folder-b/newnameforfile2.php [L,R=301]
RewriteRule ^folder-a/(.*)$ http://www.yoursite.com/folder-b/$1 [L,R=301]
</IfModule>
You probably need to put both rules in place: one for the empty folder and one for other files, and then the catch-all at the bottom. This is not tested:
RewriteEngine On
RewriteRule ^folder-a/info.php http://www.yoursite.com/folder-b/new-info.php [L,R=301]
RewriteRule ^folder-a/ http://www.yoursite.com/folder-b/ [L,R=301]
RewriteRule ^folder-a/(.*)$ http://www.yoursite.com/folder-b/$1 [L,R=301]

how to redirect all www traffic with htaccess to equal non-www pages

Question is pretty simple I guess, but I fail to do this myself.
I have a website on http://website.com and want to 301 redirect ALL requests on www.website.com to http://website.com/ but make sure all directories and filenames stay the same (so not all requests end up at the homepage).
So whatever people may type (like www.website.com/something.here) should 301 redirect to website.com/something.here
How to do this?
Add the below to your .htaccess file. Should do the trick.
RewriteCond %{HTTP_HOST} ^yoursite.com
RewriteRule (.*) http://www.yoursite.com/$1 [R=301,L]
This requires that the rewrite engine is on...
<IfModule mod_rewrite.c>
############################################
## enable rewrites
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^yoursite.com
RewriteRule (.*) http://www.yoursite.com/$1 [R=301,L]
</IfModule>

Resources