I am having a little headache with htaccess, I have the following urls:
www.site.com/products
www.site.com/contacts
They are working but I want to redirect the subdirectories that don't exist to the index page, per example:
www.site.com/contacts/asdasdasd
to
www.site.com
I've already a statement on .htaccess for the product details
www.site.com/details/product/123
www.site.com/(page name)/(product name)/(id)
Above your current rules in your .htaccess file in the root of your site, try the following:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(products|contacts)/[^/]+$ / [R=302,L]
This redirects any "subdirectory" of the products or contacts parent directories, that do not exist, to the document root.
Change the 302 (temporary) redirect to a 301 (permanent) redirect when you are happy it's working OK. (302 redirects are not cached by the browser, so makes testing easier.)
Related
I have a Detroit iOS & Android Mobile App Development website that only has one web page : index.html.
The source code of the site is here.
Instead of showing a 404 error page, I want to redirect the user to thefirstprototype.com if they try to go anywhere else or try to put anything after.
For eg:
mail.thefirstprototype.com takes the user to just thefirstprototype.com
thefirstprototype.com/mail takes the user to just thefirstprototype.com
I know it's possible to do it using a .htaccess in the root folder, but I am just not sure how. There are a lot of tutorials showing how to do it between different domains, but nothing to my specific case. How do I do it?
Thanks
Edit1: Please note that I am not using any CMS like Wordpress. I am just plain FTP to push a static HTML, CSS, JS webpage to the hosting server
Try the following:
DirectoryIndex index.html
RewriteEngine On
# Redirect non-canonical hostnames (eg. mail)
RewriteCond %{HTTP_HOST} !^example\.com$
RewriteRule ^ http://example.com/ [R=302,L]
# Redirect 404 to root
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . / [R=302,L]
However, whether this catches requests for the mail. subdomain will depend on whether that subdomain points to the same place as your main domain. (For cPanel shared hosting, that is not necessarily the case.)
Change the 302 (temporary) redirect to 301 only once you have tested that this works OK - to avoid potential caching issues associated with 301 (permanent) redirects.
As an added bonus, you could redirect any direct requests for index.html back to the root. For example, add the following between the above two rule blocks:
# Remove "index.html" if requested directly
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^index\.html$ / [R=302,L]
The condition that checks against the REDIRECT_STATUS environment variable is to ensure we don't get a redirect-loop since mod_dir internally rewrites the request to index.html.
So this is what I'm trying to accomplish, and I'll do my best to explain...
Through the .htaccess file, I'm attempting to redirect all pages of one site to another site, but ONLY if the URL doesn't exist. If the URL exists, then do nothing. Load that URL. If the URL doesn't exist, redirect to the homepage of another site.
So, going to www.johnny.com/boxing (no files) or even just www.johnny.com (no files) would redirect to www.johnpunches.com, since neither of those URLs work. And that would work for any URL on www.johnny.com that doesn't exist. It would simply redirect to the index page of www.johnpunches.com
However, going to www.johnny.com/members (where there is a working directory) would actually go to that URL and not redirect, because the page exists.
So, again, if the URL exists, then do nothing. Load that URL. If the URL doesn't exist, redirect to the homepage of another site.
RewriteEngine On
# File does not exist
RewriteCond %{REQUEST_FILENAME} !-f
# Directory does not exist
RewriteCond %{REQUEST_FILENAME} !-d
# Redirect to new domain if the above were missing
RewriteRule ^(.+)$ https://www.google.com/$1 [NC,QSA]
Change out google.com to whatever domain you are redirecting to.
Okay, I was able to get some assistance from another thread and put this together:
RewriteEngine on
RewriteCond %{http_host} ^johnny.com [nc]
RewriteRule ^(.*)$ http://www.johnny.com/$1 [r=301,nc,l]
RewriteRule !^folder($|/) http://www.johnkicks.com [L,R=301]
In the example above, the "folder" can be replaced with any directory that works. The second and third line force the www version of the site before the final rule has run so that any URL that DOES work, like johnny.com/folder would redirect to www.johnny.com/folder.
Two questions:
Did I do it correctly? Does the order of my rules work? Should I be doing it any differently?
Would I have to duplicate that fourth line for every working directory?
I have 5 URLs on a subdomain website http://subdomain.example.com, and I want them to redirect to 5 other URLs on my main website, https://www.example.com/.
Important: URLs do NOT have the same structure!
Example:
http://subdomain.example.com/url1 should redirect to https://www.example.com/ipsum
http://subdomain.example.com/url2 should redirect to https://www.example.com/lorem
etc.
How can I handle that?
UPDATE:
There is a play folder (name of the subdomain) which contains the subdomain website files and a htdocs folder which contains the www website files.
Here is the .htaccess file in my play folder:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Since the subdomain has it's own .htaccess file, you don't need to specify the hostname as part of the redirect. And since you already have mod_rewrite directives in the subdomain's .htaccess file, you should also use mod_rewrite for these redirects (to avoid conflicts). Otherwise, you'll need to specify these redirects one-by-one.
Try the following at the top of your subdomain's /play/.htaccess file. Note that this needs to go before the existing directives in the file.
# Specific redirects
RewriteRule ^url1$ https://www.example.com/ipsum [R=302,L]
RewriteRule ^url2$ https://www.example.com/lorem [R=302,L]
The above would match a request for http://subdomain.example.com/url1 and redirect accordingly, etc.
Note that the RewriteRule pattern (regular expression) does not start with a slash when used in a per-directory (.htaccess) context.
Note that these are 302 (temporary) redirects. Change them to 301 (permanent) - if that is the intention - only once you have confirmed they are working OK (to avoid caching issues).
Try this...
Redirect 301 http://subdomain.example.com/url1 https://www.example.com/ipsum
Redirect 301 http://subdomain.example.com/url2 https://www.example.com/lorem
I want to redirect mysite.com/blog/ to mysite.com/blogs/. However this redirect should not apply to other pages inside blog directory. For example, mysite.com/blog/article-1 should not redirect to mysite.com/blogs/article-1
First I tried simple 301 redirect but unfortunately it was redirecting all the content. Then I tried the following code but it didn't work out.
RewriteCond %{REQUEST_URI} ^/blog/$
Rewriterule ^(.*)$ http://www.easydestination.net/blogs/ [L,R=301]
Any idea how to do it with htaccess?
A simple 301 redirect is not working in this instance - For example:
Redirect 301 /oldpage http://www.mysite.co.uk/newsubdir/newpage
The site is dynamic and the .htaccess is already renaming pages to search engine friendly URL's from URL's containing a query string.
RewriteRule ^(.*)/(.*)$ index.php?page_name=$1&sub=$2 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)([^/])$ http://www.mysite.co.uk/$1$2/ [R=301,L]
When we are using these 301 redirects like above in the same .htaccess (at the bottom), the pages are redirecting but the query string is getting added to the end of the URL frustratingly and we have not figured out why or how to prevent it.
After 301 redirect, URL is looking like:-
http://www.mysite.co.uk/newsubdir/newpage/?page_name=old-page&sub=
...Causing a 404 error - it's just the query string added on the end of the URL's that is breaking the redirect.
Please can anyone advise on what needs to be done to fix this?
Thanks
Add question mark ? at the end of URL to prevent existing query string to be copied to a new URL:
Redirect 301 /oldpage http://www.mysite.co.uk/newsubdir/newpage?
But since you are already using mod_rewrite, I would recommend utilising it for this task as well (place this rule above your other rewrite rules):
RewriteRule ^oldpage$ http://www.mysite.co.uk/newsubdir/newpage? [R=301,L]
redirect 301 /oldfile.htm http://www.example.com/newfile.htm
Use it. Working fine.