I have moved site from one framework to another and now I would like to redirect important pages (around 20). How to redirect them using apache rewrite? Here are examples:
Old: http://mydomain.com/ba/stream.php?kat=15
New: http://www.mydomain.com/bs/about-us/our-partners
Old: http://mydomain.com/ba/stream.php?kat=29
New: http://www.mydomain.com/bs/catalogues/it-catalogue
Also, after important pages are listed, I'd like to redirect all remaining links in form:
http://mydomain.com/ba/whatever-is-here
to
http://www.mydomain.com/bs/
Following TerryE's suggestion, I'll attach my latest code which is not working :)
RewriteCond %{QUERY_STRING} ^kat=15$
RewriteRule ^stream\.php$ http://www.mydomain.com/bs/about-us/our-partners [R=301,L]
One more note: I use this code to redirect root domain to www subdomain:
RewriteCond %{HTTP_HOST} ^mydomain\.com$ [NC]
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [R=301,L]
I have placed this root to www code below code for specific pages redirection, and when I test http://mydomain.com/ba/stream.php?kat=15 in browser, I am redirected to http://www.mydomain.com/bs/ba/stream.php?kat=15
Adnan,
You need to add the following lines to your DOCROOT/.htaccess. This assumes that you don't have any .htaccess file in your ba subdirectory. (See Tips for debugging .htaccess rewrite for an explanation of why.)
RewriteEngine On
RewriteBase /
#
# Redirect kat 15 to the Partners page
#
RewriteCond %{QUERY_STRING} ^kat=15$
RewriteRule ^ba/stream\.php$ http://www.mydomain.com/bs/about-us/our-partners? [R=301,L]
#
# Redirect all other kat ids to the corresponding bs page
#
RewriteCond %{QUERY_STRING} ^kat=(\d+)$
RewriteRule ^ba/stream\.php$ http://www.mydomain.com/bs/%1? [R=301,L]
Note:
the rule matches against the UPI less the leading path (in this case /)
you need to have the trailing ? on the replacement string to suppress the existing query parameters.
the %n parameters pick up the match variables from the last successful condition match.
Related
I have this apache rule in my .htaccess file:
RewriteCond %{HTTP_HOST} ^testURL.localhost.local
RewriteRule ^index.php$ /_testURL/index.php [L]
How would I write this so that testURL2.localhost.local or any other subdomain would be rewritten to the corresponding directories?
For instance testURL2.localhost.local would be rewritten to _testURL2/index.php etc
I already tried the option below but I didn't get the intended result:
RewriteCond %{HTTP_HOST} ^testURL
RewriteRule ^cms.php$ /_%1/cms.php [L]
With your shown samples, attempts; please try following htaccess rules file. I have posted 2 sets of htaccess rules file here, you have to use ONLY one set at a time.
1st solution: This is specifically for host testURL.localhost.local and will look for either index.php OR cms.php only in UI.
RewriteEngine ON
RewriteCond %{HTTP_HOST} ^(?:www\.)?(testURL)\.localhost\.local$ [NC]
RewriteRule ^((?:index|cms)\.php)$ _%1/$1 [NC,L]
2nd solution: A Generic solution, where it will look for anyvalue.localhost.local and it will add anyvalue in path while rewriting, also it will look for any php files in uri.
RewriteEngine ON
RewriteCond %{HTTP_HOST} ^(?:www\.)?([^.]*)\.localhost\.local$ [NC]
RewriteRule ^([^.]*\.php)$ _%1/$1 [NC,L]
NOTE1: Also please make sure to clear your browser cache before testing your URLs.
NOTE2: Please keep these rules at the top of your htaccess rules file.
NOTE3: I am also additionally/optimally matching www. in host in case you don't want it you could remove it (?:www\.)? part in condition.
I want to have one URL still being served from a subdirectory on the old domain but redirect all others from the parent directory to the new domain.
www.olddomain.co.uk/directorya/directoryb -> no redirect
www.olddomain.co.uk/directorya -> www.newdomain.co.uk/directorya
This works if the URL is typed with a trailing slash
RewriteCond %{REQUEST_URI} !^/directorya\/directoryb [NC]
RewriteRule ^(.*)$ http://www.newdomain.co.uk/ [L,R=301]
So http://www.olddomain.co.uk/directorya/directoryb/ works but http://www.olddomain.co.uk/directorya/directoryb fails.
Tried added $ at the end and other connotations and tried
RewriteEngine on
RewriteRule !^myspecialdirectory/mynextdirectory($|/) http://newdomain.example%{REQUEST_URI} [L,R=301]
from Redirect entire site except one directory to a new site - apache .htaccess
What do I need to do to get http://www.olddomain.co.uk/directorya/directoryb/ or http://www.olddomain.co.uk/directorya/directoryb to redirect? (while all other http://www.olddomain.co.uk pages go to http://www.newdomain.co.uk
For reasons I don't understand http://www.olddomain.co.uk/directorya/directoryb goes to Google and doesn't redirect
Try using THE_REQUEST variable and make sure this this very first rule:
RewriteCond %{THE_REQUEST} !\s+/+directorya/directoryb [NC]
RewriteRule ^ http://www.newdomain.co.uk/? [L,R=301]
Test it after clearing your browser cache.
I'm looking to redirect each and every page on the old domain to a single page on the new domain. It should also redirect both the www and non-www versions. Every single request to the domain old.com, whatever it may be, should lead to www.new.com root.
old.com/1.php
www.old.com/2.php
old.com/2.php
old.com/links/index.php
old.com/about-us.html
old.com/?string=1
should all redirect to:
www.new.com/
I'm looking for a .htaccess solution.
You can use RedirectMatch in the old.com vhost
RedirectMatch permanent .* http://www.new.com/
Redirect appends the trailing portion of the matched URI to the redirection URI but with RedirectMatch it's up to you to choose the parts (if any) that you want to transfer using parentheses and $number references.
If the old and new domains must be aliases for the same document root on disk then you'll probably have to use mod_rewrite
RewriteEngine on
RewriteCond %{HTTP_HOST} old\.com$
RewriteRule .* http://www.new.com/ [L,R=301]
This has already been answered, but here's the code altogether containing helpful comments so people have a reference to look back on later should they forget what it does.
Put this in your .htaccess file:
## Each and every page on old domain redirects to single page
## By appending question mark to new domain, query strings are removed
RewriteEngine on
RewriteCond %{HTTP_HOST} old\.com$
RewriteRule .* http://www.new.com/? [L,R=301]
You can also redirect to a sub directory on another domain, as was the case that I needed to do like this:
## Each and every page on old domain redirects to single page
## By appending question mark to new domain, query strings are removed
RewriteEngine on
RewriteCond %{HTTP_HOST} old\.com$
RewriteRule .* http://www.new.com/sub-directory/? [L,R=301]
This will handle all your redirects including query strings. Excuse the formatting, I'm on a phone :)
RewriteCond %{HTTP_HOST} old\.com$ RewriteCond %{QUERY_STRING} .+
RewriteRule (.*) http://www.new.com$1?%{QUERY_STRING} [L,R=301,QSA]
RewriteCond %{HTTP_HOST} old\.com$
RewriteRule (.*) http://www.new.com$1 [L,R=301,QSA]
I am trying to redirect old asp pages on a domain name so they link to their respective pages on the CakePHP version (using 1.3). The domain name is the same. These redirects are being added so results in search engines go to the new Cake page.
I have a bunch of Redirect 301's in my /.htaccess file (app/.htaccess and app/webroot/.htaccess are default).
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
Redirect 301 /contacts.asp http://domain/contacts
Redirect 301 /users.asp http://domain/users
</IfModule>
But for some reason, when I go to any of the urls to test the redirects, it appends an extra query string parameter. For example:
Going to: http://domain/contacts.asp results in http://domain/contacts?url=contacts.asp
So the redirect is working but it is appending the url query string parameter. I don't want to completely remove all query string parameters because some of the old asp links have query string parameters that I would also like passed to the corresponding Cake page.
I believe the "url" query string parameter is coming from the app/webroot/.htaccess file as seen:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>
Note that when I put all my 301 redirects into my Virtual hosts file, the url parameter is not appended. I would like to keep all these redirects in my .htaccess file. How can I prevent this url query string parameter from being appended?
This looks like a case of mod_rewrite and mod_alias stepping over each other. The URI processing pipeline doesn't end when a Redirect directive is applied, it continues through the pipeline and mod_rewrite does its thing. Unfortunately, the end result isn't always what you want. You could just stick with mod_rewrite and drop the Redirect directives.
You can remove the 2 Redirect directives and add these 2 RewriteRules above the ones that map requests to the app/webroot:
RewriteRule ^contacts\.asp$ http://domain/contacts [L,R=301]
RewriteRule ^users\.asp$ http://domain/users [L,R=301]
I am trying to rewrite the following url:
the subdomain should match any subdomain. same for the TLD.
both: http://car.example.com/ and http://cat.example.co.uk should be rewritten
http://subdomain.example.com/some/dir
to
http://subdomain.example.nl/some/dir
and
http://example.com/some/dir
to
http://exampkle.nl/some/dir
(also with www. adress)
but my knowledge of htaccess and rewrite rules in general aren't good enough for this :(
I hope one of you knows the solution.
ps. I did try a search ;)
The challenge comes with having to detect and account for four different possible domain patterns:
example.com → example.nl
example.co.uk → example.nl
sub.example.com → sub.example.nl
sub.example.co.uk → sub.example.nl
So, what this ruleset does is checks that the TLD is not .nl (preventing a loop from occurring), then pulls the subdomain, www or not, off the front (read as "capture anything other than a dot followed by a dot, optional), followed by the base domain, followed by a dot. We don't have to match the entire URL, since we aren't keeping the TLD.
RewriteEngine On
RewriteCond %{HTTP_HOST} !example\.nl$
RewriteCond %{HTTP_HOST} ^([^.]+\.)?example\.
RewriteRule ^ http://%1example.nl%{REQUEST_URI} [NC,L,R=301]
The RewriteRule's ^ matches any URL, then inserts the contents of the first set of parens in the preceding RewriteCond (the subdomain) with %1, and completes the rewriting by appending the requested path and flags to ignore case, make this the last rule, and redirect with a search-engine-friendly 301, ensuring the rewritten URL appears in the user's browser. Any query string (text appearing after a ? in the URL) is automatically included by default.
Try this:
EDIT: See changes to subdomain, using %1 to capture from RewriteCond
RewriteEngine On
# Check if the hostname requested is subdomain.example.com or empty
# Now we attempt to capture the subdomain with (.*)? and reuse with %1
RewriteCond %{HTTP_HOST} ^(.*)?example.com$ [NC]
RewriteCond %{HTTP_HOST} !^$
# Rewrite it as subdomain.example.nl and redirect the browser
RewriteRule ^(.*) http://%1example.nl$1 [L,R,NE,QSA]
# Note: With the above edit for %1, this part should no longer be necessary.
# Then do the same for example.com, with or without the www
RewriteCond %{HTTP_HOST} ^(www\.)?example.com$ [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^(.*) http://www.example.nl$1 [L,R,NE,QSA]