subdomain including htaccess redirect - .htaccess

I have a subdomain code.domain.com that redirects to domain.com/code
I want to redirect for example: code.domain.com/hello/world.html to domain.com/code/?path=hello/world.html
This is what I have so far:
RewriteEngine ON
RewriteRule ^code/(.+)$ http://domain.com/code/?path=$1
But I am not sure what is wrong with it, it doesn't seem to work.
Thanks.

Use these rules in the root directory htaccess file (not the code directory) (you can also point the code.domain.com to the root directory instead if you want) :
RewriteEngine on
RewriteCond %{HTTP_HOST} ^code.domain.com$
RewriteRule ^(.+)$ /code/?path=$1

Related

Rewrite Directory Based on Subdomain

I have a folder export that is accessible to all my subdomains:
/export/sub1/...
/export/sub2/...
/export/sub3/...
Right now, regardless of what subdomain you're on, you can see all of the content by changing the directory in the url.
It's not a security issue, but more of a canonicalization concern, but I'd like to use an .htaccess file to rewrite the folders so people see a modified path that matches up with their subdomain:
sub1.domain.tld/export/... is served from /export/sub1/...
sub2.domain.tld/export/... is served from /export/sub2/...
sub3.domain.tld/export/... is served from /export/sub3/...
How can I do this?
You can use this generic rule in site root .htaccess:
RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} ^(?!www\.)([^.]+) [NC]
RewriteRule ^/?export/(.*)$ /export/%1/$1 [L,NC]

301 Redirect including Subdomain and Folder

I have two domains:
subdomain.domain.tld
subdomain.domain.tld2
They both are linking to the same ROOT Folder, lets call it "start".
So if I enter one of the both urls, It will open the index file in "start".
Now I have a subfolder in this "start" folder, lets call it "subfolder".
I can open this subfolder by entering
subdomain.domain.tld/subfolder/ OR
subdomain.domain.tld2/subfolder/
I wish that if someone calls
subdomain.domain.tld/subfolder/
it will 301 to
subdomain.domain.tld2/subfolder/
Also, if someone would call
subdomain.domain.tld/subfolder/subxyz/page=123
it will 301 to
subdomain.domain.tld2/subfolder/subxyz/page=123
I created a .htaccess in the "subfolder" with the following code, but its not working. I tried also a bunch of other Redirect Codes that I found here on Stackoverflow, but none is working for me so far. Any hel is welcome!
RewriteCond %{HTTP_HOST} ^http://subdomain.domain.tld/subfolder/[NC]
RewriteRule ^(.*)$ http://subdomain.domain.tld2/subfolder/[L,R=301,NC]
You can use :
RewriteEngine on
#--If http_host is "subdomain.domain.tld"--#
RewriteCond %{HTTP_HOST} ^subdomain.domain.tld$
#-- And the uri is /subfolder/--#
RewriteCond %{REQUEST_URI} ^/subfolder
#--Then, redirect the uri to another host --#
RewriteRule ^(.+)$ http://subdomain.domain.tld2/$1 [QSA,L,R=301]

htaccess redirect if filename equals something

I'd like to set a redirect (preferably with RewriteCond) so that if the requested file is index.php regardless of directory, it will be redirected to another site.
So visiting /index.php or/files/index.php or /stuff/index.php (etc.) will all redirect you to another domain.
Here is a general way to do it. This rule set should be placed in the .htaccess file in the root directory:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} index\.php/? [NC]
RewriteRule .* http://otherdomain.com/ [R=301,L]
Redirects permanently any URL that holds index.php in any position, like
http://mydomain.com/index.php or
http://mydomain.com/any/folder/quantity/index.php or
http://mydomain.com/any/folder/quantity/index.php/any/folder/quantity/
To
http://otherdomain.com/
That's it. You don't explain much so nothing is passed to the other domain, just as you say in your question:
...redirected to another site.
These rules should do it (when placed inside /.htaccess file):
RewriteEngine On
RewriteRule (?:^|/)index\.php$ http://otherdomain.com/ [R=301,L]

Redirecting Subdomain to Subfolder

I try to add the following logic to a .htaccess file:
subdomain.domain.xx make a redirect to subdomain.domain.xx/subdomain/
xy.domain.xy redirect to xy.domain.xy/xy/ (it should work with every subdomain without adding new rewrites)
i found a lot of solutions to redirect subdomain.domain.xx -> domain.xx/subdomain, but nothing like i need... below you find the code i try out.
Hope someone can help me. Thanks.
RewriteEngine on
RewriteBase /
RewriteRule ^([^.?]+[^.?/])$ $1/ [R]
RewriteCond %{HTTP_HOST} ^(www)?.domain.ch/$
RewriteRule .* http\://%1$1.domain.ch/%1$1 [I,R=301]
Place this in the .htaccess file of your subdomain's root directory:
DirectoryIndex index.html
RewriteEngine on
Redirect permanent /index.html http://subdomain.domain.xx/subdomain/

htaccess in subfolder cant rewrite to file

In the root directory there's an htaccess file that directs everything that lands on it to goto a diff domain
RewriteEngine on
RewriteRule (.*) http://www.domain.com/
http://www.domain.com/123 = http://www.domain.com/
I have a folder named 'admin' within the root dir with another htaccess document inside it, I want that htaccess doc to point the root of itself to 'admin.php'
RewriteEngine on
RewriteRule ^$ /admin.php
http://www.domain.com/admin/ = http://www.domain.com/admin/admin.php
But no matter how I do it, I either get 500 Error or the rewrite rule from the main root doc over-rides it.
Hope that makes sense, thanks.
The easiest way to fix this (there are several) is probably exclude the admin directory from the RewriteRule in the root using a condition like this:
RewriteEngine on
RewriteCond $1 !^admin/.*
RewriteCond $1 !^admin\.php
RewriteRule (.*) http://www.domain.com/

Resources