.htaccess: Redirect all pages except one - regex

I'm trying to setup my .htaccess file to redirect from oldsite.com to newsite.com, with just one exception: If the user visits http://oldsite.com/cal then I want it to display the cal.html file in the root directory.
Here's the current .htaccess file that I've got (which doesn't work):
Options +FollowSymlinks -MultiViews
RewriteEngine On
# this page can be served .. (not working)
RewriteRule /cal http://oldsite.com/cal.htm [L,NC]
# .. but rewrite everything else (this works fine)
RewriteCond %{HTTP_HOST} ^(www\.)?oldsite\.com [NC]
RewriteRule ^(.*) http://newsite.com/$1 [R=301,L,NC]
What do I mean by it doesn't work? Well, it just redirects http://oldsite.com/cal to http://newsite.com/cal instead of displaying http://oldsite.com/cal.html

Change it to this:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteRule ^cal/? cal.htm [L,NC]
RewriteCond %{HTTP_HOST} ^(www\.)?oldsite\.com [NC]
RewriteRule ^ http://newsite.com%{REQUEST_URI} [NE,R=301,L]
Notes
if oldsite and newsite live on different servers, you don't even need the RewriteCond.
you could also invert the order of the rules, in which case the main rule would be RewriteRule ^(?!cal/?$) http://newsite.com%{REQUEST_URI} [NE,R=301,L]

Related

These rules don't work and I don't know why

I am trying to do a temporary redirect from one site to another.
I got this code from this site > redirect one site to another
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^ukhorseracingtipster.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.ukhorseracingtipster.com$
RewriteRule (.*)$ https://www.fromthestables.com/$1 [R=301,L]
</IfModule>
But it doesn't work.
I've put it at the top of the .htaccess file and bottom. Doesn't make a difference.
What do I need to do to redirect one site to another

Why does my htaccess not rewrite properly

I am having a problem with my htaccess file, and can't figure out if it's my configuration, or something like server cache which is messing with my URLs.
My file is the following
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^website.com$
RewriteCond %{HTTPS} off
RewriteRule (.*) https://website.com/$1 [R=301,L]
RewriteRule ^([a-z]+)\/?$ $1.php [NC]
The first rule is to move www to non-www, it works.
The second rule is to move http to https, it also works.
the third rule is to make URL /anyFile call anyFile.php, but keep a lovely URL.
The problem is that it calls anyFile.html, not PHP, and if I remove said HTML file then I get 404.
Yes it works typing anyFile.php in the URL, but I would like to not have .php in the URL.
If it is not obvious enough, it is supposed to work for any file name, not just a single one.
Any and all help is much appreciated.
Replace your .htaccess rules with this code:
Options -MultiViews
RewriteEngine On
RewriteBase /
# single rule for http->https and www removal
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^(.*)$ https://website.com/$1 [R=301,L]
# hide .php extension
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([a-z]+)/?$ $1.php [NC,L]
I'm recently learn how to do htaccess too, so I'm still a novice but I would write
RewriteRule ^([a-z]+)\/?$ helloWorld.php [NC]
to redirect it to the helloWorld.php file.
The $1 is a get parameter.

mod rewrite for pretty urls and also hide index.php

How can i mod-rewrite to obtaining a pretty url like below
example.com
contains a form to get a text value and pass it another file for processing that file is in directory as web but i need to change that as pretty urls
example.com/web/index.php?url=mydomain.com
to
example.com/web/mydomain.com
example.com/web/newdomain.com
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 %{THE_REQUEST} ^[A-Z]{3,}\s/+web/index\.php\?url=([^\s&]+) [NC]
RewriteRule ^ /web/%1? [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^web/(.+?)/?$ /web/index.php?url=$1 [L,QSA]

confused about .htaccess subdomain rewrite (not redirect)

I have 2 domains pointing to the root of my site:
http://site1.com
http://site2.com
both point to http://dXXXXXXX.domain.com/
I have each site set up to look for its files in a subdirectory (of the same name) so
http://site1.com files are located in http://dXXXXXXX.domain.com/site1/
http://site2.com files are located in http://dXXXXXXX.domain.com/site2/
I would like to create a special subdomain for site1 (and possibly site2) that actually points to a folder in the main directory (a forum that both domains can share). I would like to mask the real url with the subdomain url like the following:
http://subdomain.site1.com masks http://dXXXXXXX.domain.com/shared_folder/ (not redirected)
my .htaccess file (in the topmost directory) looks like this:
Options +FollowSymLinks
RewriteEngine On
# Rewrite "www.domain.com -> domain.com"
<IfModule mod_rewrite.c>
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
</IfModule>
# redirect site1.com to /site1 [folder]
# redirect site2.com to /site2 [folder]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} ^([^0-9]+)$ [NC]
RewriteRule ^(.*)$ /%1/$1 [QSA,L]
#i think this should work but it doesn't
#RewriteCond %{HTTP_HOST} ^subdomain\.site1\.com$ [NC]
#RewriteRule ^$ shared_folder [P]
also, if possible, I would like to redirect the absolute path to each domain to my preferred path as follows:
http://dXXXXXXX.domain.com/site1/ redirects to http://site1.com
http://dXXXXXXX.domain.com/site2/ redirects to http://site2.com
not sure that anyone would ever find those links, but I'd like to keep everything clean by not having multiple access points for the same piece of content (with the exception of intentionally shared css or forum software)
Thanks in advance for any help or insight you may have for me
I think I figured it out (as in it hasn't broken yet!)
Options +FollowSymLinks
RewriteEngine On
# Rewrite/Redirect "www.domain.com -> domain.com"
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
# Rewrite/Redirect "domain.com/foo -> domain.com/foo/"
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/|#(.*))$
RewriteRule ^(.*)$ /$1/ [R=301,L]
# Redirect/Pass through "subdomain.site1.com to /shared_folder/"
RewriteCond %{HTTP_HOST} ^subdomain\.([\w.]+)$ [NC]
RewriteRule ^(.*)$ /shared_folder/ [L]
# Redirect/Pass through site1.com to /site1 [folder]
# Redirect/Pass through site2.com to /site2 [folder]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} ^([^0-9]+)$ [NC]
RewriteRule ^(.*)$ /%1/$1 [QSA,L]
So this works, but I'm not sure about the placement of my rules: if it's better to tackle the special subdomain case before the domains themselves, as in the order I have now; or if RewriteRule ^(.*)$ /shared_folder/ [L]is better written as RewriteRule ^$ /shared_folder/ [L]...
If you have any insight as to good or better practices I would appreciate it, thanks.

htaccess: redirect old domain and all pages to a new domain

I know that there is a lot of examples on Stackoverflow but I still miss something.
I'm trying to redirect http://old.domain.com/fr/ to http://brand.new-domain.com/fr/ with the following rules, but that doesn't work:
# Enable Rewrite Engine
RewriteEngine On
RewriteBase /
# Add a trailing slash to paths without an extension
RewriteCond %{REQUEST_METHOD} !=POST
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule ^(.*)$ $1/ [L,R=301]
# Redirect domain
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} ^old.domain.com [OR]
RewriteCond %{HTTP_HOST} ^other-old.domain.com [NC]
RewriteRule ^(.*)$ http://brand.new-domain.com/$1 [r=301,L]
# Remove index.php
# Uses the "exclude method"
# http://expressionengine.com/wiki/Remove_index.php_From_URLs/#Exclude_List_Method
# This method seems to work best for us, you might also use the include method.
# http://expressionengine.com/wiki/Remove_index.php_From_URLs/#Include_List_Method
# Exclude root files
RewriteCond $1 !^(index\.php) [NC]
# Exclude EE folders
RewriteCond $1 !^(assets|ee-admin|images|templates|themes|fr|nl)/ [NC]
# Exclude user created folders
RewriteCond $1 !^(assets|css|img|js|swf|uploads)/ [NC]
# Exlude favico, robots, ipad icon
RewriteCond $1 !^(favicon\.ico|robots\.txt|pple-touch-icon\.png) [NC]
# Remove index.php
RewriteCond %{QUERY_STRING} !^(ACT=.*)$ [NC]
RewriteCond %{QUERY_STRING} !^(URL=.*)$ [NC]
RewriteRule ^(.*)$ /index.php?/$1 [L]
It correctly redirect when I call the root URL, but not when I call a page. What am I doing wrong?
Thanks in advance!
Pv
When writing mod_rewrite rules, the rules get applied in the order that they appear.
To redirect an old domain to a new domain, you'll want that rule to be first in your .htaccess or httpd.conf file — all other rules should appear after it.
If you only want to redirect a certain directory, the following rule will do so, while allowing the rest of the site to function normally:
<IfModule mod_rewrite.c>
RewriteEngine On
# Redirect Only Matching Directories
RewriteCond %{REQUEST_URI} ^/(fr|fr/.*)$
RewriteRule ^(.*)$ http://brand.new-domain.com/fr/$1 [R=301,L]
</IfModule>
If you want to redirect the entire site, the following rule will do so:
<IfModule mod_rewrite.c>
RewriteEngine On
# Redirect Entire Site to New Domain
RewriteCond %{HTTP_HOST} ^old.domain.com$ [OR]
RewriteCond %{HTTP_HOST} ^other-old.domain.com$ [NC]
RewriteRule ^(.*)$ http://brand.new-domain.com/$1 [R=301,L]
</IfModule>
If you care about letting crawlers know your content has moved and want to make the transition as seamless as possible, be sure to keep the 301 Redirect flag in the RewriteRule.
This will ensure that users and search engines are directed to the correct page.
While we're on the subject, as part of the EE 2.2 release, EllisLab now "officially" offers limited technical support for removing index.php from ExpressionEngine URLs.
Simply add or update your code to the following, making sure to consider any rules you may already have in place:
<IfModule mod_rewrite.c>
RewriteEngine On
# Removes index.php
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
# If 404s, "No Input File" or every URL returns the same thing
# make it /index.php?/$1 above (add the question mark)
</IfModule>
Try to use the following ruke as the first one:
# Redirect domain
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} ^old.domain.com [OR]
RewriteCond %{HTTP_HOST} ^other-old.domain.com [NC]
RewriteRule ^(.*)$ http://brand.new-domain.com/$1 [R=301,L]
Also mind the upper case R with is the short form for the lower case redirect.
Have you tried using mod_alias simple redirect instructions (a core module that you have), before trying the hacky-mod-rewrite thing?
I would do a VirtualHost with ServerName old.domain.com and in this VH I would add this rule:
Redirect /fr http://brand.new-domain.com/fr
from doc:
Then any request beginning with URL-Path will return a redirect request to the client at the location of the target URL. Additional path information beyond the matched URL-Path will be appended to the target URL.
So get a separate VirtualHost for brand.new-domain.com (with ServerName brand.new-domain.com) and in this one do not set the Redirect Rule.
If you still want to handle the 2 domains in the same VirtualHost then you'll have to use mod-rewrite as even RedirectMatch cannot check the request domain on the query.