Apache htaccess URL rewrite guidance please - apache

aI have a site, that is mirrored across many domain names. Let's say there are 26 of them, and they are:
www.aaa.com
www.bbb.com
... etc ...
www.zzz.com
All these domains are virtual hosts that reference the same document root and hence obey the same htaccess file.
What I now want to do is turn off all of them except for three. I only want to keep:
www.aaa.com
www.eee.com
www.uuu.com
I want all the other domains to forward to www.aaa.com, including the full original URL parameters and query string.
How do I set up rewrite rules in my htaccess file such that the following 301 redirects will happen:
www.bbb.com/thispage/ -> www.aaa.com/thispage/
www.kkk.com -> www.aaa.com
www.eee.com (not redirected - keep processing htaccess rules below)
www.uuu.com/thispage/ (not redirected - - keep processing htaccess rules below)
I hope I have made things as clear and simple as possible!
Thanks for any help.

Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^(www\.)?(aaa|eee|uuu)\.com$ [NC]
RewriteRule ^ http://www.aaa.com%{REQUEST_URI} [R=301,L]

Related

htaccess shows 404 error rather than finding index.php

I'm having some issues with a shady URL rewriting
I want to turn http://localhost:81/es/index.php into http://localhost:81/index.php?lengua=es with my .htaccess in order to help the page SEO
This is my current .htaccess
<FilesMatch ".*\.(log|ini|htaccess)$">
deny from all
</FilesMatch>
Options -Indexes
RewriteEngine On
RewriteBase /
FallbackResource "index.php"
RewriteRule ^(en|es|pt)?/?(.*)?$ $2?idioma=$1 [QSA,L]
I have checked that they work with htaccess tester and they're working as expected but when I browse the page it shows a "File not found." error (I do have a index.php, I do not have a es/index.php)
Since my output URL is http://localhost:81/index.php?lengua=es I don't understand why is it not working
I would suggest breaking this in four rewrite rules:
RewriteEngine on
# Redirect to add the trailing slash to language directory
# http://example.com/es > http://example.com/es/
RewriteRule ^/?(en|es|pt)$ /$1/ [R=301,L]
# Redirect to remove `index.php`
# http://example.com/es/index.php > http://example.com/es/
RewriteRule ^/?(en|es|pt)/index\.php$ /$1/ [R=301,L]
# Handle requests for the base language directory
# http://example.com/es/ > http://example.com/index.php?idioma=es
RewriteRule ^/?(en|es|pt)/$ /index.php?idioma=$1 [QSA,L]
# Handle requests for php files within the language directory
# http://example.com/es/foo.php > http://example.com/foo.php?idioma=es
RewriteRule ^/?(en|es|pt)/(.+\.php)$ /$2?idioma=$1 [QSA,L]
I would remove RewriteBase / because I believe that is the default in the root .htaccess file anyway.
I would remove FallbackResource "index.php" because you shouldn't need it based on the examples you have provided. If you keep it, the examples in the documentation show it starting with a slash: FallbackResource /index.php. You should also test without it because it has the potential to conflict with the rewrite rules.
I always like to start rewrite rules with an optional slash ^/? (rather than just ^) so that they can be used both in .htaccess and in httpd.conf without modifications.
The rule in the question makes everything optional including the language code. Rather than (en|es|pt)? my rules use (en|es|pt) so that they don't match if the language code isn't in the URL.
Rather than make the slash after the language directory optional, my rules do different things when it is present and when it is not.
In your rule (.*)? is exactly equivalent to the simpler (.*). I changed it to (.*\.php) so that it only matches PHP files.

Remove '/home' directory from URL

People i got 'www.domain.com/home'. I want delete from name '/home'. Just on url should left 'www.domain.com'.
I tested .htaccess but this not works. I see server automatic delete 'index.html' from URL. Some rules works in my htaccess but the replacing a names in URL - not.
Somebody know how to delete '/home'? I just spent all morning to find solution, but nothing working...
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteRule ^home/?$ / [L,R=301,NC]

htaccess folder rewrite

I've been reading multiple posts on here about htaccess folder rewriting but none seem to fit my question (properly).
My question is:
I have 2 sub folders on the server, website1 and website2.
When a user goes to www.foo.com I wish the visual url to remain the same but want the server URI to go to /website1/ where it will load the index.php for website1
I then want the same thing only when a user goes to www.bar.com again the url does not change but this time it links to /website2/ where it will load the index.php for the 2nd website.
Would really appreciate some help with this as I'm still learning about rewrites. Examples with explanations would be highly appreciated. Also any advice of best practice (if their is any) would also be appreciated.
KingCrunch is right -- the proper way to setup such environment is to use <VirtualHost> directive in Apache config file.
If, for whatever reason this needs to be dona via rewrite and .htaccess .. then you need mod_rewrite to be enabled and .htaccess files to be allowed to contain rewrite rule (AllowOverride directive).
Here are the rules:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
# rule #1
RewriteCond %{HTTP_HOST} =www.foo.com
RewriteCond %{REQUEST_URI} !^/website1/
RewriteRule (.*) /website1/$1 [L]
# rule #2
RewriteCond %{HTTP_HOST} =www.bar.com
RewriteCond %{REQUEST_URI} !^/website2/
RewriteRule (.*) /website2/$1 [L]
This code is to be placed in .htaccess file in root folder. If placed elsewhere (e.g. configuration or virtual host context) some tweaking may be required.
Fist rule is for www.foo.com and second for another domain name. These rules are pretty much the same. We tell Apache to check domain name (via {HTTP_HOST} request variable), and if it matches our domain rewrite (internal redirect) URL into one folder deeper. The second condition is to prevent a rewrite loop (to not to rewrite already rewritten URL). It is necessary as Apache, after executing rewrite, goes to the next rewrite iteration (that is how it works), and this condition is required to stop the loop.
Useful link: http://httpd.apache.org/docs/current/rewrite/
I believe that you need to use only RewriteCond and RewriteRule directives. Take a look 'Virtual User Hosts' at http://httpd.apache.org/docs/1.3/misc/rewriteguide.html.
The logical is the same. (I think.)

Redirecting PDF links from another domain using htaccess

We have two domains, let's call them first.com and second.com
We have a directory in second.com called reports, where all our PDFs are located, but we would like to these same PDFs accessible from first.com as well.
Can we redirect let's say first.com/reports/84839049.pdf to second.com/reports/84839049.pdf using htaccess?
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^domain\.com
RewriteRule (.*) http://domain1.com/$1 [R=301, L]
Yes.
redirect /requested/url http://second.com/result/url
http://httpd.apache.org/docs/1.3/mod/mod_alias.html#redirect
You may want to consider using mod_rewrite though, unless you asked for an .htaccess configuration specifically because you have no access to the server configuration and mod_rewrite is disabled or not loaded.
http://httpd.apache.org/docs/current/mod/mod_rewrite.html
http://webdesign.about.com/od/mod_rewrite/qt/site_redirects.htm
You'll need some grasp of regex for mod_rewrite, but it can make configuration of the redirects a lot faster than having to add a redirect for every file on your site(s).

How can I rewrite this URL with Apache?

I want to be able to access sitenamehere.com/folder/index?a=something by visiting sitenamehere.com/folder/something in my address bar.
How can I do this?
I've looked into mod rewrite but I don't understand it.
mod_rewrite is an Apache (web server) extension not related to PHP. You'll want to create a file called .htaccess and include the following line:
RewriteEngine On
RewriteRule ^folder/(.*) /folder/index.php?a=$1
http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html
Your .htaccess or httpd.conf.
# Turn on URL rewriting
RewriteEngine On
# Installation directory
RewriteBase /
# Your rule
RewriteRule ^([^/]+)/([^/]+)/?$ $1/index?a=$2 [L]
This assumes you want where folder is to be mapped over to where folder is in your example. If you want to match the literal folder, just replace the first capturing group with it (and add it to the replacement).