Redirect to page with .htaccess - .htaccess

I have a basic website with pages and directories. Currently the main landing page is at https://www.example.com/index.php
I would like add a marketing landing page which is located at:
https://www.example.com/app/index.php
I would like for my visitors to land on the marketing page when they come to my site's domain. Naturally I thought of .htaccess redirect, but it doesn't seem to work.
//Rewrite to www
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain.com[nc]
RewriteRule ^(.*)$ https://www.example.com/$1 [r=301,nc]
// Redirect landing page
Redirect /app/index.php /index.php
Curently I'm getting error:
Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.

You can use these rules in your site root .htaccess:
Options +FollowSymLinks
RewriteEngine on
# Rewrite to www
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,NE,L]
# Redirect landing page
Redirect ^app/index\.php$ /index.php [L,NC]
Also note that .htaccess only supports comments beginning with #

Related

.htaccess Rewrite code to 301 redirect my URL path to fix canonical issues

I discovered recently that a Hostgator tech inserted rewrite code in my .htaccess file which 302 redirected all of my pages to https://example.com even though I wasn't using that URL path for my sitemap or internal links. Now I have a mix of URL paths indexed by Google and lots of duplicate pages.
Trying to clean it up and use one URL path -> https://example.com
This is the code they inserted:
RewriteCond %{HTTP_HOST} example\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://example.com/$1 [R,L]
RewriteEngine on
RewriteOptions inherit
Can I simply change the [R,L] to [R=301,L] so that now everything will be a permanent redirect? I will also update all internal links and sitemap to use this URL path.

Htaccess too many redirects only for wordpress blog

I have follwing htaccess for redirecting to www, https and index.html to /:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteBase /
RewriteRule ^index\.html$ / [NC,R,L]
On the website I use a wordpress blog on /blog, which gives this error if I try to access it:
ERR_TOO_MANY_REDIRECTS
I can fix this by swaping the rule order, first comes redirect to www and then https, but then the redirect to www does not work
Okay I found the issue. As a programmer you really should check from time to time what people who have some access to site settings are doing.
In the wordpress login for the admin in settings -> general a custom domain was entered which was https://example.com/blog etc. which means wordpress blog redirected back to the version without www.
The fix is to add www to the wordpress setting.

Redirecting in .htaccess non to www

For some odd reason when I visit my site without the "www." it redirects properly. However when I try to visit mywebsite.com/test.php it will redirect to mywebsite.com/public_html/test.php.
For some odd reason it adds "public_html" to the redirect breaking the page
My .htaccess is on the same level as the public_html place. Below is the code im using in the .htaccess file.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mywebsite\.com [NC]
RewriteRule ^(.*)$ http://www.mywebsite.com/$1 [L,R=301,NC]

Prevent access of sub-/addondomain via htaccess

I have 2 domains: main.com and addon.net
On my shared hosting account I create an addon-domain foraddon.net which automatically creates a folder in the main-domain's directory as well as a subdomain.
I want to change the accessability of the addon domain via the maindomain:
http://addon.main.com
http://main.com/addon.net/
Now both serve the index.html from addon.net
Both URLs should result in a "404 - not found" error.
What I have right now on main.com/.htaccess
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?main.com$ [NC]
RewriteCond %{REQUEST_URI} ^/addon.net/(.*)$
RewriteRule ^(.*)$ 404.html [L]
And in addon.net/.htaccess
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*)\.main.com$ [OR]
RewriteRule ^(.*)$ http://www.main.com/ [R=301,L]
And now everything redirects to main.com:
http://main.com/addon.net/ redirects to http://www.main.com
http://addon.main.com redirects to http://www.main.com
http://addon.net redirects to http://www.main.com
My question: which rules should I add to which .htaccess-file in order to get the desired results:
addon.main.com redirecting to main.com/404.html
main.com/addon.net redirecting to main.com/404.html
addon.net serving addon.net/index.html
If you want to redirect access from anything except addon.net (and redirect the other requests to a 404 page), all you need to do is use these lines in the .htaccess file inside addon.net :
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www\.)?addon\.net$ [NC]
RewriteRule - /404.html [L]

301 Redirect all pages to root, not index.html, and also redirect all non-www to www

I've been looking for a precise answer to this question for awhile but couldn't find it.
I've launched a one page website in place of an old website with many pages. Now, I want to redirect everything to www.domain.com. The page uses index.html as the homepage, but I don't want to redirect to that, I just want to redirect to the www.domain.com root.
I tried using:
RewriteRule ^.+$ / [R=302,NC,L]
But that just broke my stylesheet and didn't redirect anything. Other solutions I've seen have redirected to the index.html but I want to redirect to the / root domain.
Also, I want to be sure to redirect all non-www pages to www pages too. Can someone please help me out?
Much appreciated
You have most likely something like this in your httpd.conf:
<IfModule dir_module>
DirectoryIndex index.php index.php3 index.html index.htm
</IfModule>
If you request a folder (and http://example.com/ is a request to a folder), it will try index.php first, then index.php3, etc, etc, and the first one that exists it will shown. You'll have to delete that from your httpd.conf if you want every request to end up as http://example.com. It'll show a directory view of your www-root folder unless that has been disabled.
Try:
# Any direct request for html/php pages
RewriteCond %{THE_REQUEST} /[^\?\ ]+\.(html?|php.) [NC]
RewriteRule ^ / [L,R=301]
That won't affect images, or style sheets, etc. Note that this matches against the %{THE_REQUEST} variable because internally the %{REQUEST_URI} gets converted to /index.html so you can't match against that.
If you want non-existent requests (which would normally result in a 404) to be redirected as well:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ / [L,R=301]
To force a "www" use:
RewiteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]

Resources