htaccess redirect only specific page, not subpages - apache

I have a website with following url structure:
www.example.com/products
www.example.com/products/productA
www.example.com/products/productB
I need to redirect www.example.com/products to www.example.com but www.example.com/products/productA and productB should still be available.
Does anyone have an idea?

At the top of your .htaccess file, using mod_rewrite:
RewriteRule ^products$ / [R=302,L]
I've used mod_rewrite here, as opposed to a mod_alias RedirectMatch directive since I assume you are already using mod_rewrite later in the file to rewrite your URLs. It is preferable not to mix redirects/rewrites from both modules.
Reference:
https://httpd.apache.org/docs/2.4/mod/mod_rewrite.html#rewriterule

Related

301 htaccess redirect from old path to new path

I want to the following redirect 301 to happen on .htaccess file for my site.
From:
www.mysite.com/view/2133/page.html
To:
www.mysite.com/node/2133
The 2133 will be a dynamic number like a * (eg www.mysite.com/view/*/page.html)so it will work for all my other pages.
Try:
RewriteEngine On
RewriteRule view/([0-9+]) /node/$1 [R=301,L]
For an explanation see: URL Aliasing, Redirection, Rewriting and Reverse Proxying using Apache HTTPD

I need a 301 htaccess redirect that will find files regardless of case

We are moving to apache from IIS. IIS did not care about case, it served pages whether it was the user capitalized some words or not. But now I'm getting 404 errors all over the place. I need a 301 redirect that will automatically search for similar pages.
For example:
NewHoMepage.htm
will redirect to
newhomepage.htm
and
News25/newnews.htm
will redirect to
news25/NewNews.htm
Our site has 25 directories with 13,000+ pages, so a pages by page redirect is out of the question.
Any help would be appreciated.
You can try this short rule in your htaccess file
RewriteEngine on
RewriteRule ^(.*)$ http://example.com/$1 [R=301,NC,L]
Because each case is different, one cannot simply redirect to lower case.
As you have mentioned you don't want to do redirects for 13k pages, RewriteMap is also out of the question.
The general solution is to use CheckSpelling on in your .htaccess file. To be more specific, you could use CheckCaseOnly on instead.
Here's the documentation for mod_spelling:
http://httpd.apache.org/docs/current/mod/mod_speling.html
Found some interesting solutions here: http://www.askapache.com/htaccess/rewrite-uppercase-lowercase.html.
Personally, I'd go with the second solution, by adding the following in the .conf file:
RewriteEngine on
RewriteBase /
RewriteMap lowercase int:tolower
RewriteCond $1 [A-Z]
RewriteRule ^/?(.*)$ /${lowercase:$1} [R=301,L]
As a side note, R=301 helps you from a SEO perspective, as search engines will update the links for your side and your pages will not be marked as duplicate content.

How to redirect to subdomain but then allow normal use of site

So I have my site, www.domain.com.
For a week or so I want to direct all traffic going direct to the site to subdomain.domain.com, a little promo page about an upcoming feature. I want visitors to then be able to continue to the site as normal though after they've read it, so a continue to www.domain.com/index.php link.
How can I do that with in the htaccess file? Everything I've tried so far messes up when clicking the continue link.
Thanks
with .htaccess you could use a 302 temporary redirect, but it would be for a whole sub folder as far as I know.
Another way would be to redirect with JS/server site language to the subdomain, create a cookie, then redirect back to www.domain.com/index.php .
the 302 redirect is explained here: How do I redirect my site using a .htaccess file?
You would need to have a .htaccess for the root folder point to your subdomain
Note that this is only possible if you enable mod_proxy in the Apache config of domain.com otherwise URL will change after redirect.
Enable mod_proxy, 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
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^index\.php$ http://subdomain.domain.com/ [L,NC,P]

Redirecting all sub-sub pages to another subpage using htaccess

I've a older site running in Apache Server which is already indexed in Google. I wish to redirect all those indexed links to my new site (As the older pages are not existing any more.)
So i wish to redirect all my sub-sub pages to my new root page
I've pages like follows
http://itdost.com/answer-now/Aerobics
http://itdost.com/answer-now/HTML
http://itdost.com/answer-now/Culture
I use the following redirect code for each one
Redirect 301 /answer-now/Engineering http://www.itdost.com/questions/
Redirect 301 /answer-now/Food http://www.itdost.com/questions/
Redirect 301 /answer-now/ASP http://www.itdost.com/questions/
But as the site structure is big, i wish to do it in a single line instead of writing a line for each redirect
Some thing like the following.
Redirect 301 /answer-now/% http://www.itdost.com/questions/
But the above code does not seems to work
In order to use regex better to use mod_rewrite which is more powerful than mod_alias.
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 /
RewriteRule ^answer-now(/.*|)$ http://www.itdost.com/questions/? [L,NC,R=301]
Try this:
RedirectMatch 301 ^/answer-now/ http://www.itdost.com/questions/

Use .htacess to redirect all files in a specific folder to a script

I want to use htacess to redirect the following examples:
www.site.com/downloads/file1.txt
www.site.com/downloads/folder/file2.txt
to
www.site.com/download?file=file1.txt
www.site.com/download?file=folder/file2.txt
Ignore that there are slashes in the query string for the sake of the example.
You can use mod_rewrite for that.
RewriteEngine on
RewriteRule "^/downloads/(.+)" "/download?file=$1" [R=301,L]
Set the proper HTTP status code for the rewrite action (301 - permanent, 307 - temporary).
As I never used mod_rewrite in .htacces, you may need to try out some more combinations. Maybe the directory name needs to be removed from the regular expression, as it is already clear from the .htaccess context - the manual should help.
RewriteEngine on
RewriteRule ^/downloads/(.*) /download?file=$1 [R=301,L]
301 - Moved Permanently
The resource has permanently moved to a different URI.
Resources:
HTTP Status Codes
http://www.helpwithpcs.com/courses/html/html_http_status_codes.htm#300
Apache mod_rewrite
http://httpd.apache.org/docs/current/mod/mod_rewrite.html