www-removal via .htaccess forwards to index.php - .htaccess

What I ultimately am trying to do is forward any URL with www to it's non-www equivalent. So https://www.baremetrics.io/academy would automatically forward to https://baremetrics.io/academy.
Instead, those www pages forward to https://baremetrics.io/index.php
And I'm stumped on why. Hoping there's something in the .htaccess file that I'm overlooking.
Here is the contents of the .htaccess file for baremetrics.io:
Header set Access-Control-Allow-Origin "*"
# Turn on the Rewrite Engine
RewriteEngine On
# If you're running in a subfolder (like http://example.com/statamic),
# add that here. E.g. /statamic/
RewriteBase /
# Protect your system files from prying eyes
RewriteRule ^(_app) - [F,L]
RewriteRule ^(_config) - [F,L]
RewriteRule ^(_cache) - [F,L]
RewriteRule ^(_content) - [F,L]
RewriteRule ^(_logs) - [F,L]
RewriteRule ^(admin/themes/[^/]*/(?:layouts|templates)) - [F,L]
RewriteRule ^(.*)?\.yml$ - [F,L]
RewriteRule ^(.*)?\.yaml$ - [F,L]
RewriteRule ^(.*/)?\.git+ - [F,L]
# This will prevent all .html files from being accessed.
# You may want to remove this line if you want to serve
# static files outside of Statamic
# RewriteRule ^(.*)?\.html$ - [F,L]
# Remove trailing slashes from your URL
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\/(\?.*)?$ $1$2 [R=301,L]
# Remove the index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA,L]
# Redirect old pages
RedirectMatch 301 ^/signup(.*)$ https://dashboard.baremetrics.io/signup$1
RedirectMatch 301 ^/dashboard(.*)$ https://dashboard.baremetrics.io$1
RedirectMatch 301 ^/stats(.*)$ https://dashboard.baremetrics.io/stats$1
RedirectMatch 301 ^/billing\.html(.*)$ https://dashboard.baremetrics.io/billing.html$1
RedirectMatch 301 ^/switch(.*)$ https://dashboard.baremetrics.io/switch$1
# No WWW
RewriteCond %{HTTP_HOST} !^baremetrics.io$ [NC]
RewriteRule ^(.*)$ https://baremetrics.io/$1 [L,R=301]
For what it's worth, the site is deployed to Heroku.

The issue is that your other rules are taking place first which causes your non-WWW redirect to not trigger.
You can move your # No WWW rule right after RewriteBase / which should fix it.
Header set Access-Control-Allow-Origin "*"
# Turn on the Rewrite Engine
RewriteEngine On
# If you're running in a subfolder (like http://example.com/statamic),
# add that here. E.g. /statamic/
RewriteBase /
# No WWW
RewriteCond %{HTTP_HOST} !^baremetrics.io$ [NC]
RewriteRule ^(.*)$ https://baremetrics.io/$1 [L,R=301]
# Protect your system files from prying eyes
RewriteRule ^(_app) - [F,L]
RewriteRule ^(_config) - [F,L]
RewriteRule ^(_cache) - [F,L]
RewriteRule ^(_content) - [F,L]
RewriteRule ^(_logs) - [F,L]
RewriteRule ^(admin/themes/[^/]*/(?:layouts|templates)) - [F,L]
RewriteRule ^(.*)?\.yml$ - [F,L]
RewriteRule ^(.*)?\.yaml$ - [F,L]
RewriteRule ^(.*/)?\.git+ - [F,L]
# This will prevent all .html files from being accessed.
# You may want to remove this line if you want to serve
# static files outside of Statamic
# RewriteRule ^(.*)?\.html$ - [F,L]
# Remove trailing slashes from your URL
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\/(\?.*)?$ $1$2 [R=301,L]
# Remove the index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA,L]
# Redirect old pages
RedirectMatch 301 ^/signup(.*)$ https://dashboard.baremetrics.io/signup$1
RedirectMatch 301 ^/dashboard(.*)$ https://dashboard.baremetrics.io$1
RedirectMatch 301 ^/stats(.*)$ https://dashboard.baremetrics.io/stats$1
RedirectMatch 301 ^/billing\.html(.*)$ https://dashboard.baremetrics.io/billing.html$1
RedirectMatch 301 ^/switch(.*)$ https://dashboard.baremetrics.io/switch$1

Related

.htaccess 301 redirect only work for 1 new link

I have .htaccess file with redirect 301 on it and placed right under the rewriteengine
<IfModule mod_rewrite.c>
RewriteEngine On
#now this is the redirect
Redirect 301 /blog.php?slug=konsolidasi-tanah-frequently-asked-questions https://jpi.or.id/blog/2021/02/11/pengertian-konsolidasi-tanah
Redirect 301 /blog/2021/02/11/konsolidasi-tanah-frequently-asked-questions https://jpi.or.id/blog/2021/02/11/pengertian-konsolidasi-tanah
</IfModule>
only 2nd redirect works, the one with the get method are not redirected, am i doing wrong?
EDITED :
this is my entire mod rewrite :
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^index\.php$ / [R=301,L]
RewriteRule ^(.*)/index\.php$ /$1/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
# Permanent URL redirect
Redirect 301 /blog.php?slug=konsolidasi-tanah-frequently-asked-questions https://jpi.or.id/blog/2021/02/11/pengertian-konsolidasi-tanah
# Permanent URL redirect
Redirect 301 /blog/2021/02/11/konsolidasi-tanah-frequently-asked-questions https://jpi.or.id/blog/2021/02/11/pengertian-konsolidasi-tanah
Redirect 301 /blog.php?slug=konsolidasi-tanah-frequently-asked-questions /blog/2021/02/11/pengertian-konsolidasi-tanah
RewriteRule ^blog/2021/02/11/pengertian-konsolidasi-tanah /blog.php?slug=konsolidasi-tanah-frequently-asked-questions
It is usually not a good idea to mix RewriteRule and Redirect 301 directives. They can conflict with each other in unexpected ways. You depend of RewriteRule so you should implement your redirects with more of them.
Redirect 301 can't redirect based on query strings (?...) in the URL, so you need to implement RewriteRules for that redirect anyway.
When you have rules for redirecting specific URLs, they should go at the top of the .htaccess file so that they take precedence over the other more general rules.
I would recommend disabling the directory index because I fear it would conflict with your RewriteRule ^index\.php$ / [R=301,L] rule.
I don't see RewriteEngine On in your .htaccess file, despite that your snippet you posted to start with has it.
Try this as your .htaccess:
# Disable index.html, index.php default functionality
DirectoryIndex disabled
RewriteEngine On
# Permanent URL redirect
RewriteCond %{QUERY_STRING} ^slug=konsolidasi-tanah-frequently-asked-questions$
RewriteRule ^blog\.php$ https://jpi.or.id/blog/2021/02/11/pengertian-konsolidasi-tanah [R=301,L]
RewriteRule ^blog/2021/02/11/konsolidasi-tanah-frequently-asked-questions$ https://jpi.or.id/blog/2021/02/11/pengertian-konsolidasi-tanah [R=301,L]
# Forward URLs without .php extension to existing PHP file
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php
# Redirect index.php URLs to the directory
RewriteRule ^index\.php$ / [R=301,L]
RewriteRule ^(.*)/index\.php$ /$1/ [R=301,L]
# Use index.php as a front controller
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]

Catch all redirect

This is the htaccess file. I'm wanting to add a catch-all 301 redirect RewriteRule.
This is not working. Any ideas?
RewriteRule ^/(.*)$1 http://www.domain.co.uk/ [R=301,L]
Here are the rest of the rewrites.
enter coOptions +SymlinksIfOwnerMatch +MultiViews
RewriteEngine On
RewriteBase /
### HTTP > HTTPS & non WWW to WWW version
# RewriteCond %{HTTPS} !on [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
# RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(\S*)\sHTTP [NC]
RewriteRule ^ http://www.domain.co.uk/%1 [NE,L,R=301]
### index.php & index to root domain
RewriteRule ^index(?:\.php)? http://www.domain.co.uk/ [R=301,L]
### special rewrite rules for shortened urls ##
RewriteRule ^section(?:\.php)?/(.*)$ /s/$1 [R=301,L]
### To remove section page if 1 which is default for most (unless products span more)
RewriteRule ^s\.php/(.*)/1/(.*) /s/$1/$2 [R=301,L]
RewriteRule ^product(?:\.php)?/(.*)$ /p/$1 [R=301,L]
RewriteRule ^article(?:\.php)?/(.*)$ /a/$1 [R=301,L]
RewriteRule ^discount(?:\.php)/(.*)$ /d/$1 [R=301,L]
### Core Jshop .php removal
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*?)\.php/(.*)$ $1.php?$2 [L,QSA]de here
Redirect 301 /blog http://www.domain.co.uk/a/8/nursing-home-news/
RewriteRule ^/(.*)$1 http://www.domain.co.uk/ [R=301,L]
If your rewrite rules are in an htaccess file then you need to remove the leading slash from your rule's pattern as RewriteRule's regex matches against a relative old path starting without a / .
Try :
RewriteRule ^(.*)$ http://www.domain.co.uk/ [R=301,L]
Make sure to clear your browser cache before testing this.

How to remove index.php in Magento 1.9 using .htaccess

I have such URL at my Magento website as http://magento.store/about and http://magento.store/index.php/about. I don't need these two pages, I need them to be properly rewrited in .htaccess file. I've tried everything which I could find at StackOverflow but nothing helps. I configured URL-rewrites in configuration using adpanel, used different solutions but the most, I got is to do these redirects but my adpanel doesn't writes any changes to the website's database (I used this solution:
RedirectMatch 301 ^/index.php/((?!admin).*) http://www.magento.store/$1
In this case it is impossible to change anything in adpanel. The usual solutions such as
RewriteEngine on
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
do not also work. They are ignored as far as I understand.
How can I solve this issue?
Here is my .htaccess file (the part about the mod_rewrite):
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteCond %{HTTP_HOST} ^magento\.store$ [NC]
RewriteRule ^(.*)$ http://www.magento.store/$1 [L,R=301]
# redirect pagination
RewriteCond %{QUERY_STRING} ^p=1$
RewriteRule ^(.+)$ http://www.magento.store/$1? [R=301,L]
RewriteRule ^pagename$ http://www.magento.store/page-name [L,R=301]
#RewriteCond %{REQUEST_URI} !/admin/
#RewriteRule ^index\.php/(.+)$ /$1 [R,L]
#RewriteRule ^index\.php/?$ / [R,L]
#RedirectMatch 301 ^/index.php/(.*)$ /$1
#RedirectMatch 301 ^/index.php/((?!admin).*) http://magento.store/$1
RewriteRule ^api/rest api.php?type=rest [QSA,L]
############################################
## workaround for HTTP authorization
## in CGI environment
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
############################################
## TRACE and TRACK HTTP methods disabled to prevent XSS attacks
RewriteCond %{REQUEST_METHOD} ^TRAC[EK]
RewriteRule .* - [L,R=405]
############################################
## always send 404 on missing files in these folders
RewriteCond %{REQUEST_URI} !^/(media|skin|js)/
############################################
## never rewrite for existing files, directories and links
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
############################################
## rewrite everything else to index.php
RewriteRule .* index.php [L]
</IfModule>
Is it possible that I have some conflicts inf my file?
You can capture the part after index.php and redirect the client with
RewriteEngine on
RewriteRule ^index\.php/(.+)$ /$1 [R,L]
RewriteRule ^index\.php/?$ / [R,L]
This redirects all requests starting with index.php/ to the URL without index.php. Additionally index.php and index.php/ are redirected to the home page.
Use below .htaccess rule:
## index.php on default domain
RewriteCond %{THE_REQUEST} ^.*/index.php
RewriteCond %{REQUEST_URI} !^/downloader.*$
RewriteRule ^(.*)index.php$ http://www.idesignmydrapes.com [R=301,L]
RewriteCond %{HTTP_HOST} ^idesignmydrapes\.com$ [NC]
Thank you, everybody, for your help. Finally, I made the solution and it is the following:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !/adpanel/
RewriteRule ^index\.php/(.*)$ http://magento.store/$1 [L,R=301]

Subfolder query string htaccess redirect

Everything I can find for query string redirects talks about something like this:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^view=home$
RewriteRule .? http://mywebsite.com/? [L,R=301]
That's great, but I'm dealing with a url that looks like this:
example.com/portfolio/?gal=16
If I test it without the "portfolio/" it will work fine, but my old site is indexed as above. Any tips on how I can make it work with my URL structure?
EDIT Current .htaccess contents
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
# Canonical Remove www
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
# Redirects
Redirect 301 /feedback http://example.com/testimonials
Redirect 301 /about http://example.com/about-us
Redirect 301 /contact http://example.com/contact-us
#futile attempts at query string redirects omitted
Thanks.
New Edit to include #anubhava's suggestions
Might as well give you the actual .htaccess content since the site is now live.
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^dev.bayshorephotography.com$ [OR]
RewriteRule (.*)$ http://bayshorephotography.com/$1 [R=301,L]
# Canonical Remove www
RewriteCond %{HTTP_HOST} ^www.bayshorephotography.com$ [NC]
RewriteRule ^(.*)$ http://bayshorephotography.com/$1 [R=301,L]
RewriteCond %{QUERY_STRING} ^gal=16$
RewriteRule .? http://bayshorephotography.com/portfolio/weddings-02/? [L,R=301]
Redirect 301 /feedback http://bayshorephotography.com/testimonials
Redirect 301 /about http://bayshorephotography.com/about-us
Redirect 301 /contact http://bayshorephotography.com/contact-us
#301 Redirect Entire Directory to remove /blog/ from all blog pages except the main archive itself
RedirectMatch 301 /blog(/.*)/ $1
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
# Old Redirects that don't work
#Redirect 301 /portfolio?gal=20 http://bayshorephotography.com/portfolio-items/weddings-04/
#Redirect 301 /portfolio?gal=29 http://bayshorephotography.com/portfolio-items/our-world-03/
#Redirect 301 /portfolio?gal=16 http://bayshorephotography.com/portfolio-items/weddings-02/
#Redirect 301 /portfolio?gal=18 http://bayshorephotography.com/portfolio-items/weddings-04/
#Redirect 301 /portfolio?gal=28 http://bayshorephotography.com/portfolio-items/album-samples-01/
#Redirect 301 /portfolio?gal=19 http://bayshorephotography.com/portfolio-items/weddings-05/
#Redirect 301 /portfolio?gal=22 http://bayshorephotography.com/portfolio-items/portraits-01/
#Redirect 301 /portfolio?gal=24 http://bayshorephotography.com/portfolio-items/our-world-01/
#Redirect 301 /portfolio?gal=17 http://bayshorephotography.com/portfolio-items/weddings-03/
#Redirect 301 /portfolio?gal=36 http://bayshorephotography.com/portfolio-items/portraits-04/
#Redirect 301 /portfolio?gal=35 http://bayshorephotography.com/portfolio-items/portraits-03/
#Redirect 301 /portfolio?gal=32 http://bayshorephotography.com/portfolio-items/weddings-01/
#Redirect 301 /portfolio?gal=25 http://bayshorephotography.com/portfolio-items/our-world-02/
#Redirect 301 /portfolio?gal=23 http://bayshorephotography.com/portfolio-items/portraits-02/
#Redirect gone /portfolio?gal=34
#Redirect gone /portfolio?gal=33
#Redirect gone /portfolio?gal=27
#Redirect gone /portfolio?gal=21
You need to change order of your rules and keep 301/302 before WP rules:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# Canonical Remove www
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
RewriteCond %{QUERY_STRING} ^view=home$
RewriteRule .? http://example.com/? [L,R=301]
# Redirects
RewriteRule ^feedback/?$ http://example.com/testimonials [L,R=301]
RewriteRule ^about/?$ http://example.com/about-us [L,R=301]
RewriteRule ^contact/?$ http://example.com/contact-us [L,R=301]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

rewrite on urls to add "/"

I know how to rewrite urls to redirect to a page.
When the "/" is not at the end of www.DomainName.com/user/myUserName the redirect works:
RewriteRule /$ /user/index.php
When I write RewriteBase / the above rule does not work, but a 404 error.
How to add a "/" at the end of www.DomainName.com/user/myUserName/ and still redirect to /user/index.php.
This can be written into the .htaccess or httpd.conf
Hey, this is the default html access that I used.
Options +FollowSymLinks +ExecCGI
<IfModule mod_rewrite.c>
RewriteEngine On
# we skip all files with .something
RewriteCond %{REQUEST_URI} \..+$
RewriteCond %{REQUEST_URI} !\.html$
RewriteRule .* - [L]
# we check if the .html version is here (caching)
RewriteRule ^$ index.html [QSA]
RewriteRule ^([^.]+)$ $1.html [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
# no, so we redirect to our front web controller
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>

Resources