Foward directory to domain with parameters stripped for dir only not site wide - .htaccess

I'm permanently forwarding a directory to its own domain, i had a wordpress setup in the directory at one time which google indexed... I've setup the redirect in my .htaccess however permalink structure is appended to the forwarded domain. So im taking everything from /sample forwarding to www.sample.com but im wanting to strip the unused variables from the url such as www.sample.com/?p=6, etc... However i need this done only for files in the /sample directory as i still have wordpress installs that need to remain intact. Also how can i not have a double forward slash after the redirect. I have:
Redirect permanent /sample http://www.sample.com
But it outputs http://www.sample.com//

Try this instead:
Redirect 301 /sample/ http://www.sample.com/?
The ? should strip query string (?p=6 etc)
The same but using mod_rewrite:
RewriteRule ^(sample(/|/.*)?)$ http://www.sample.com/$1? [NC,R=301,L]

Related

How to redirect non www to www for prod website

I've tried to add the item below in .htaccess(wamp64\bin\apache\apache2.4.37\conf) and enabled Use URL Rewriting in Joomla however when I try to type abc.com in search engine address bar it will still redirect me to search page instead of redirecting me to the website. I've also performed checking and confirmed that .htaccess file is readable, the webpage is work when go to https://www.abc.co m however redirection still not work. I just need the page to redirect from http://abc.co m to https://www.abc.co m Is there any other way to do it? Or I'm doing it wrongly?? Please help.
RewriteCond %{HTTP_HOST} ^abc.com [NC]
RewriteRule ^(.*)$ https://www.abc.co m/$1 [L,R=301]
The redirect itself works great. The issue is where you've put the file.
.htaccess files are meant to be in the root folder and sub-directories of your website, not in the config files for Apache. Think of them as configuration files the server will find on-the-fly as it serves up documents, not static configurations loaded when the server starts (which are found in the "conf" folder you referred to). (See https://httpd.apache.org/docs/current/howto/htaccess.html)
For WAMP, the root folder is typically a folder called "www" and is normally located at c:/wamp/www. See Where is the web server root directory in WAMP? More commonly, though, this folder is called "public_html" (such as with LAMP, XAMP, MAMP, or default Apache installs).

Redirect all subdomains to subdomain at different domain

Hi I need to redirect all subdomains in a domain to the same subdomain but at a different domain. The best way im guessing is through a htaccess file but im not sure how the file would be.
Example:
sd1.example.net ---> sd1.example.com
sd2.example.net ---> sd2.example.com
sd3.example.net ---> sd3.example.com
But I need this to be done for all of the subdomains in example.net. Thanks.
If you have an Apache server running on example.net and the requests for all the subdomains look in the same parent directory you can do something like the following:
RewriteEngine On
### Find the subdomain part (it will be available in %1)
### Use one of the RewriteCond-s and delete the other one
# Only redirect subdomains, not plain example.net
RewriteCond %{HTTP_HOST} ^(.*)\.example\.net$
## Redirect both subdomains and plain example.net (uncomment to enable)
#RewriteCond %{HTTP_HOST} ^(.*\.)?example\.net$
# Find the path requested (it will be available in $0)
# This rule does not attempt to match the domain, only the path
# Redirect subdomain and path to example.com
RewriteRule ^.*$ http://%1.example.com/$0 [L]
I haven't tested this so it might be missing query strings, etc. It will also undesirably redirect https:// to http://. As long as you have a single .htaccess file that can affect all your subdomains this should work, or at least be a very good starting point. Check out Apache's mod_rewrite documentation for more information about how this works.
EDIT
Having recently wanted to do exactly this myself recently, I have worked out a short .htaccess file that does the trick:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*\.)?olddomain\.com$
RewriteRule ^(.*?/)?public_html/(.*)?$ "http\:\/\/%1newdomain\.org\/$2" [R=301,NE,L]
It assumes the following file structure:
.htaccess
public_html/
+-content
lots/
+-public_html/
| +-content
of/
+-public_html/
| +-content
subdomains/
+-public_html/
+-content
My main site (newdomain.org) is in /public_html/. I have a number of subdomains, e.g. subdomains.newdomain.org which is in /subdomains/public_html/. This keeps all the files of each my subdomains completely separate from each other and my main site. (My hosting service recommends /public_html/, /public_html/subdomains/ but that means each subdomain is also accessible at newdomain.org/subdomains/ which is not what I want). The only restriction this gives me is that I can never have a subdomain called public_html, which I think you'll agree is perfectly acceptable.
The flags on the rule are as follows:
R=301 - Redirect with a 301 Moved Permanently code. You can change the code if you don't need a permanent redirect, e.g. 302.
NE - No Encoding - Don't URI encode the new address, i.e. keep % as %, not %25
L - Last - Stop processing rules
Note that the .htaccess file must be in the root directory of your web server, not in the directories with your content files. This is because the rewrite rule works at the file system level, not the URL address level.
An address:
any.subdomain.olddomain.com/any/address.html?any=query&you=like
is changed to:
any.subdomain.newdomain.org/any/address.html?any=query&you=like

Subdomain Points to Subfolder and need .Htaccess to Redirect to a Wordpress Multi-Site Sub-Site

Ok, since a subdomain has to point to a real place, I created the subdomain pra.aquizone.net to point to aquizone.net/blogs/links/pra. The Wordpress (WP) Multi-site (MU) that I want to point to is aquizone.net/blogs/pra where that directory is virtually handled by the WP MU and the .htaccess under aquizone.net/blogs.
I am assuming that I can change the .htaccess file under aquizone.net/blogs/link/pra to point to aquizone.net/blogs/pra and have the subdomain wind up resulting at aquizone.net/blogs/pra. Only everything I have tried does not work. Here is my currect .htaccess file:
# Use PHP5 Single php.ini as default
AddHandler application/x-httpd-php5s .php
RewriteEngine On
RewriteRule ^pra.*$ http://aquilone.me/blogs/pra/ [R=301,L]
The AddHandler was placed there by the subdomain creation and taking it out or leaving it makes no difference. It does not work. You can go to http://aquilone.me/blogs/links/pra/ and see it does not redirect.
How can I change this to work correctly?
Well, depending on your provider, you can point your subdomain towards the same directory of your top level domain, i.e.:
example.com -> /public_html/
this.example.com -> /public_html/
But your question is not clear about what you are trying to do. Is it a subdomains network or is it a subfolders network? If subdomains, try to change the directory your subdomain points to as in the example above, and create the blog inside wp-admin/network/ If subfolders, then you don't need the subdomain, unless you really want to redirect it to a subfolder just like an easier to remember shortcut, in that case you don't need a RewriteRule, WP takes care of the virtual subfolder.
I figured out what had happened. Anything from the aquizone.net/blogs forward was being handled by Wordpress and the .htaccess setup at this subfolder. Therefore, having the redirect point to aquizone.net/blogs/links/pra and in that folder have the .htaccess to then point it where I want was never being seen.
To fix this, I moved the "links" directory to aquizone.net/links. Placing inside the ./pra/ folder with the .htaccess file. The subdomain redirects to this folder and then the .htaccess redirects this to the correct wordpress multisite subdirectory.
Of course it would be immensely easier if you could access a multisite by a hard link (for example aquizone.net/blogs/index.php?id=2) and could set the subdoman to redirect directly to that. But what I have is working.

.htaccess redirect from subdirectory to another domains subdirectory accordingly

I am trying to make a redirect from my primary domain to an secondary domain, but only if the primary domain's request is to a sub directory.
The sub directory I want to redirect from is FTP, so if the user makes the following request:
http://www.site1.com/FTP/free/50b694124bd63/SaMple+PicTure.PnG
it would be transformed to
http://www.site2.com/FTP/free/50b694124bd63/SaMple+PicTure.PnG
but if the user makes a request that does not involve the FTP folder, the user will not be redirected. Like so:
http://www.site1.com or http://www.site1.com/somethingelse/
I am, however; a bit lost when it comes to making .htaccess files. What I have tried to do so far is:
# Redirect users
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^ftp(.*)$ http://site2.com/FTP/$1 [L,R=301]
</IfModule>
Any directions or samples would be great :)
No need to use the rewrite engine for simple redirects. I think you just want to use the Redirect directive:
Redirect /FTP http://www.site2.com/FTP
By default, this will result in a "temporary" redirect response (HTTP status 302). If you're sure the URL of the second site will never change, you can cause a "permanent" redirect response (HTTP status 301) by adding the permanent argument:
Redirect permanent /FTP http://www.site2.com/FTP
Also, note that the path of URLs is case-sensitive. If you want http://www.site1.com/ftp to also redirect, you will either need to add a rule with the lowercase path,
Redirect /ftp http://www.site2.com/FTP
or use mod_speling.

htaccess folder to subdomain rewrite

How can example.com/blog seamlessly show the content of blog.example.com without a redirect?
Not this:
RewriteRule ^/blog http://blog.example.com [R=301,L]
Thanks.
In order for this to occur, both the domain and subdomain would have to be running from the same directory of files. Then you could just rewrite the /blog path to the same file that the subdomain is using to run. Depending on your permissions, it could be possible to specify a ../ path to the file that it's using if you get the directories right.
However, anytime you specify a full domain path in a rewrite rule, Apache will always redirect the request. You can't get around that.
if example.com/blog and blog.example.com(subdomain) both points to same directory(blog). Then you can directly access the specified path without any redirection.

Resources