.htaccess routing to subdirectory on server? performance/loading times - http

I wonder if it is possible to use an .htaccess file to rout my domain directly to a subdirectory on my server?
I bought a webhosting package from a regular webhoster where my domain.com is connected to my root directory of my server. I wonder if it is somehow possible to upload a .htaccess file to my root directory on my server that automatically routs domain.com (/index.php or /index.html) to domain.com/some-directory/ …
And of course I don't want the addressbar to update to domain.com/some-directory. I want my regular domain just to grab its files from the subdirectory instead of my root directory.
Just for better overview on my server.
So again, when calling mydomain.com it renders index.html (or .php) from my root directory on my server. However I want mydomain.com to render /subdirectory/index.html.
If this is possible, how can I do it? Is this bad in some way? Is there something I didn't think of, e.g. are there longer loading times or anything like that with that approach?
Thank you in advance.
Matt
update: or is this possible with a DNS setting on my web-admin-panel. I'm able to edit all DNS entries.

Try putting this in the htaccess file in your document root:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/some-directory/
RewriteCond %{DOCUMENT_ROOT}/some-directory%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/some-directory%{REQUEST_URI} -d
RewriteRule ^(.*)$ /some-directory/$1 [L]
Keep in mind if you try to access a directory and are missing a trailing slash, mod_dir will redirect the browser to the URL with the trailing slash and will expose "/some-directory/".
EDIT
Additionally, the 2 -f and -d conditions are to make sure the request is actually for a resource that exists. This isn't required, and if you have further htaccess files with rewrite rules, you probably don't want these. They ensure that if someone requests some bogus URL, the 404 message will be handled outside of the /some-directory/ directory. Otherwise the 404 message will say something along the lines of:
The requested URL /some-directory/blahblahblah was not found on this server.
Exposing that everything is being routed through /some-directory/.

Related

Redirect ex.com/en/* to ex.com/* using .htaccess

I've just moved my website from sub-directory to root , But the search engines still have the old urls listed in there Index. Till the time fresh URL's get listed.
Is there any way to redirect those requests to that main domain.
Old URL: example.com/en/testurl
New URL: example.com/testurl
thanks
I had my site under subfolder and I decided to move to the root level of the domain.
The main challenge was to keep all articles on the same path as a subfolder.
Guess what it’s possible through RewriteCond in Apache web server.
This is possible through “.htaccess”
Ensure to take a backup of an existing file (.htaccess)
Add the below line and save the file
RewriteEngine on
RewriteBase /
RewriteRule ^subfolder/(.*)$ /$1 [R=301,NC,L]
Remember to replace "subfolder" with your previous installation directory.
In the last line, I used 3 flags.
R=301 (permanent redirect)
NC (Case insensitive)
L (last - stop processing rules)
This worked for me and I hope for you too.

Redirecting Dynamic URLs with Multiple IDs using .htaccess (Wordpress)

I've just moved my site onto Wordpress, which means the URL structure has now changed. I'd like to use a Rewrite Rule in the .htaccess file to redirect dynamic URLs with multiple IDs in one single statement, but am not having any success. An example is:
OLD URL: example.co.uk/seasons/season.php?ID=1819
NEW URL: example.co.uk/seasons/1819
The statements I have tried in .htaccess are:
RedirectMatch 301 ^/seasons/season.php?ID=(.*).htm$ example.co.uk/seasons/$1
and
RewriteCond %{QUERY_STRING} ^?ID=1$
RewriteRule ^/seasons/season.php$ example.co.uk/seasons/? [R=301,L]
In neither case, the redirect fires. Is there something about a Wordpress .htaccess file that I'm not considering, or is the error with the statements I'm attempting. I'm using .htaccess 301 redirects for http to https and non-www to www without issue.
This is the fixed version of the approach you chose, to make an external redirection:
RewriteEngine on
RewriteCond %{QUERY_STRING} (?:^|&)ID=(\d+)(?:&|$)
RewriteRule ^/?seasons/season\.php$ https://example.co.uk/seasons/$1 [R=301,QSD]
Since both, the old and new URLs use the same host name you can simplify that:
RewriteEngine on
RewriteCond %{QUERY_STRING} (?:^|&)ID=(\d+)(?:&|$)
RewriteRule ^/?seasons/season\.php$ /seasons/$1 [R=301,QSD]
It is a good idea to start out with a 302 temporary redirection and only change that to a 301 permanent redirection later, once you are certain everything is correctly set up. That prevents caching issues while trying things out...
These rules will work likewise in the http servers host configuration or inside a dynamic configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a dynamic configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).

Limit web folder access to specific URL

I am hosting two different websites using one hosting package.
My initial website is self-built and runs from the public_html directory of my host let's call this 'http://www.website1.co.uk'
I also have a Word Press blog running in a folder under the public_html - so '/folder_2', let's call this 'http://www.website2.co.uk' - this URL currently points to '/folder_2' and will load the blog when entered in to a browser.
At the moment the blog is also accessible by entering 'http://www.website1.co.uk/folder_2/'.
I would like to be able to limit access to 'folder_2' to just 'website2.co.uk' - preventing somebody typing in the 'website1.co.uk/folder_2' path and being able to view the blog.
What's the best way to achieve this, bearing in mind the folder in question is running a Word Press blog.
Try adding this in your .htaccess file in folder_2
RewriteEngine On
RewriteCond %{http_host} !website2.co.uk
RewriteRule .* http://website2.co.uk
that would redirect anyone who goes to website1.co.uk/folder_2 to website2.co.uk, but if you just want them hit a 404 put this in .htaccess instead
RewriteEngine On
RewriteCond %{http_host} !website2.co.uk
RewriteRule .* - [R=404,L]
1# move folder_2 out of public_html(you really should do that).
2# rewrite all request of 'http://www.website1.co.uk/folder_2/' to 404 in htaccess
Check below link:
Redirection Subfolder Htaccess

needed a suggestion in redirecting old URLs to a new URL

Earlier i have installed my code in the main domain itself. for instance,
www.abcd.com/xyz/page.html
there are about 300 pages indexed by google with the old URL.
Now i have removed that code from the main domain and installed the code in Sub-Domain. So it make all the URLs indexed by Google and all the backlinks are pointing to the old URL.
Now i need to point all the old invalid URL to new Valid URL when Users clicks on the old URL.
Please suggest how to handle this.
Thanks a lot...
If you are using Apache, I suggest that you use mod_rewrite as it allows to send the HTTP status 301 Moved Permanently, informing search engines that the contents of your website has been moved to a new location (rather than just deleted).
In a .htaccess file:
RewriteEngine on
RewriteRule (.*) http://newdomain.mysite.com/$1 [R=301,L]
The first line enables URL "rewriting", the fancy term for redirections of all kind. The second line is decomposed like this:
RewriteRule is the directive to match URLs and change the place they point to;
(.*) is a regular expression matching any requested file path (without an initial slash)
http://newdomain.mysite.com/$1 is the place where you want to send your visitors, and $1 is expanded to the previously matched path
[R=301,L] tells Apache to send the 301 Moved Permanently HTTP status code, and that it's the last RewriteRule that can match this request (it's useful only when you have multiple RewriteRules but it doesn't hurt to have it anyways).
The next time crawlers visit your site, they will notice the HTTP status and update their links to your new address. You should have it set up as soon as possible before Google thinks your whole site went 404.
If you are using Apache as your web server you can use mod_rewrite or mod_alias command. This is more a server job and not a programming job.
You can put the following in a .htaccess file or in the vhost container.
mod_rewrite
RewriteEngine On
RewriteRule ^xyz/page.html$ new-page.php [L,NC,R=301]
See: http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriterule
mod_alias
Redirect permanent xyz/page.html http://domain.com/new-page.php
See: http://httpd.apache.org/docs/current/mod/mod_alias.html#redirect
PHP
You can use a Location header to perform a redirection:
header('Location: http://domain.com/new-page.php', true, 301);
Java
public void HttpServletResponse.setHeader(String name, String value)
See: http://docstore.mik.ua/orelly/java-ent/servlet/ch05_06.htm
This should be handled by the server, preferably using an htacces redirect
PHP:
header('Location: new.url.com');

How do I create a redirect from a subdirectory to the root domain?

I am trying to redirect all requests to domain.com/drupal to domain.com, including all sub directories in /drupal.
I have seen several answers telling me how to accomplish the opposite of this with .htaccess, but nothing to go this way. I have tried the following line in .htaccess-
RewriteRule /drupal/* ^/(.*)
as well as several variations of the above, based on those answers, but haven't had any luck.
Thanks!
Try this line:
RewriteRule /drupal/(.*) /$1 [QSA,L]
Let me get this straight ... You have an installation of Drupal in <DocumentRoot>/drupal/. You do not want to alter the drupal installation directory, nor you want to change DocumentRoot in your webserver config. You want to redirect any request, for example /foobar.php, into the drupal directory, resulting in maybe /drupal/foobar.php. And all that without exposing the whole stuff to the user. Right so far? OK, I can only assume that you have an Apache webserver, else .htaccess would not work...
First, make sure that you actually are allowed to use .htaccess, so check on the relevant AllowOverride directive in your apache config.
Then try it this way in your <DocumentRoot>/.htaccess:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/drupal.*
RewriteRule ^/(.*)$ /drupal/$1 [P]
RewriteCond ensures that you do not run into an infinite loop. The first part of the RewriteRule is always the URL requested by the client. We prefix the part matched inside the parentheses with /drupal/ and force it to be a proxy request via [P] so that apache would only do an internal redirect (instead of sending the client a "Document has moved" redirection code).
BTW: I did not test it. I may have typos in the code. Read http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html#rewriterule for more information.

Resources