.HTACCESS Redirection Problem: index.php Before URL Slug - redirect

I am trying to achieve a simple http to https redirect and www to non-www. The problem is that .htaccess puts "index.php" before the url slug, resulting in a server error. Here is what happens:
WRONG BEHAVIOUR:
http://example.com/url-slug -> https://example.com/index.php/url-slug
DESIRED BEHAVIOUR:
http://example.com/url-slug -> https://example.com/url-slug
Note: I want all queries to redirect to the index.php page in the main directory, unless the requested file exists on the server. I'd like to achieve this without changing the url in the browser, which causes the server to crash.
(Objective: www -> non-www & http -> https)
CURRENT .HTACCESS SETTINGS:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
What am I doing wrong?

Your http to https 301 redirect should be at the top before other internal rewrite rules
RewriteEngine On
#http to https
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#www to non-www
RewriteCond %{HTTP_HOST} ^www\.(.+)$
RewriteRule (.*) https://%1%{REQUEST_URI} [NE,L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Make sure to clear your browser cache before testing this change.

Related

htaccess redirect all variants to https

Using CWP, I have setup a webserver powered by Apache + NginX + Varnish.
I want the following domains samples to open https variants, i.e. https://example.com
example.com
www.example.com
http://www.example.com
http://example.com
https://www.example.com
https://example.com
Hosted websites are developed using CodeIgniter, so index.php also needs to be bypassed.
My current .htaccess is:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
I have tried:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
RewriteCond %{HTTPS} !=on
But it's not working. The following variants makes websites inaccessible on all variants
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
and tried some other variants also even after referring to articles here at Stack Overflow, none is working for me.
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
says too many redirects
There is no effect of any other variant. http and https works fine in them, but no redirection to https is achieved.

Website needs to convert from http to https redirect

I am working on a website and I have secured it using an SSL Certificate. The Https: works when manually typed in https://www.example-direct.co.uk it shows no error and has a secure icon. However, if we enter example.co.uk it directs it to the http: without the secure key.
I am just wondering if I can do a simple redirect? This is the redirect we currently have set up in .htaccess. What do I add here to redirect to http: to Https://
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
# Redirect non www. to www.
RewriteCond %{HTTP_HOST} !^www\.example-direct\.co\.uk
RewriteRule (.*) http://www.example-direct.co.uk/$1 [R=301,L]
# Remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1? [R=301,L]
# Remove .php ONLY if requested directly
RewriteCond %{THE_REQUEST} (\.php)
RewriteRule ^(.+)\.php /$1 [R=301,L,QSA]
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php

rewriteRule or redirect of url in htaccess

I'm trying to redirect or rewrite a specific page. In my .htaccess I've tried both of these lines but neither worked. The press-releases-news/?lang=en page exists on my site but not /press-releases-news/?lang=fi, so I'm trying to avoid the 404 that results. Appreciate some pointers here.
Redirect 301 /press-releases-news/?lang=fi/ /press-releases-news/?lang=en
RewriteRule /press-releases-news/?lang=fi/ /press-releases-news
Placed directly after RewriteEngine On
<IfModule mod_rewrite.c>
RewriteEngine On
Redirect 301 /press-releases-news/?lang=fi/ /press-releases-news/?lang=en
RewriteRule /press-releases-news/?lang=fi/ /press-releases-news
RewriteBase /
RewriteRule ^(.+)\.aspx?$ handleRedirect.php [L,NC]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Try:
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{QUERY_STRING} lang=fi
RewriteRule ^press-releases-news http://yourdomain.com/press-releases-news [R=301]

URL redirection to subdomain without changing URL in address

I am new to Linux and want to use .htaccess or proxy for the URL redirection leaving URL unchanged with apache2.
Now i want to redirect my main domain http://example.com/test to my subdomain http://test.example.com without changing the URL in address bar.
Kindly help me in giving me some guidelines with .htaccess or proxy or some other..
If you don't want the URL to change you either need a Reverse proxy set-up between the two virtual hosts / domains, or if example.com is just an alias, same IP and served from the same Apache virtual host, you could use an internal rewrite. More info HERE
your can also try this. for more infor go HERE
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^/?(index.\php)?$ /category [L]
This only works for requests for http://domain.com/ or http://domain.com/index.php, and you'd get served the content at http://domain.com/category without changing the URL in the address bar. You can probably even lose the RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC] line if you don't need it.
there is also a helpful resource here
RewriteEngine on
RewriteBase /
######## example.com ########
RewriteCond %{HTTP_HOST} ^.*example.com$ [NC]
RewriteCond /www/quux-foo.com/www/root/v1%{REQUEST_URI} -d
RewriteRule ^(.*[^/])$ %{REQUEST_FILENAME}/ [L]
RewriteCond %{HTTP_HOST} ^.*quux-foo.com$ [NC]
RewriteCond %{REQUEST_URI} ^/v1/
RewriteRule ^(.*)$ - [F]
RewriteCond %{HTTP_HOST} ^.*example.com$ [NC]
RewriteCond /www/quux-foo.com/www/root/v1%{REQUEST_URI} -f [OR]
RewriteCond /www/quux-foo.com/www/root/v1%{REQUEST_URI} -d
RewriteRule ^(.*)$ v1/$1 [L]
RewriteCond %{HTTP_HOST} ^.*example.com$ [NC]
RewriteCond %{REQUEST_URI} !^/v1/
RewriteRule ^(.*)$ v1/index.php [L]
######## example.com ########

How do I get my contact form to work after mod_rewrite?

I've built a site using CMS Made Simple. I have to point the host URL for everything EXCEPT the admin page and the contact forms. I was able to successfully do this with mod_rewrite to the config.php and changing up the .htaccess, but now my contact forms are no longer working (one in the footer and one on the contact page). Here is what my .htaccess file looks like:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^energyfa.ipower.com$ [NC]
RewriteCond %{REQUEST_URI} !^/ai/admin/
RewriteRule ^(.*)$ http://accimpress.com/$1 [R=301,L]
# 301 Redirect all requests that don't contain a dot or trailing slash to
# include a trailing slash
# except for form POSTS
RewriteCond %{REQUEST_URI} !/$
RewriteCond %{REQUEST_URI} !\.
RewriteCond %{REQUEST_METHOD} !^POST$
RewriteRule ^(.*) %{REQUEST_URI}/ [R=301,L]
# Rewrites urls in the form of /parent/child/
# but only rewrites if the requested URL is not a file or directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?page=$1 [QSA]
Is there a reason my POST exception might not be working? Any suggestions will be much obliged. The website is: http://energyfa.ipower.com/ai/
Thanks,
Cory
try changing your .htaccess from
# .htaccess for CMS made simple
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !/$
RewriteCond %{REQUEST_URI} !\.
RewriteCond %{REQUEST_METHOD} !POST$
RewriteRule ^(.*) %{REQUEST_URI}/ [NE,R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteRule ^(.+)$ index.php?page=$1 [QSA]
# EOF
to
# .htaccess for CMS made simple
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !$ <--- to put the "/" ---->
RewriteCond %{REQUEST_URI} !\.
RewriteCond %{REQUEST_METHOD} !POST$
RewriteRule ^(.*) %{REQUEST_URI} [NE,R=301,L] <--- to put the "/" after } ---->
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteRule ^(.+)$ index.php?page=$1 [QSA]
# EOF
it work for us
I see that the action on your form is set to http://energyfa.ipower.com/ai/contact-us/. What I believe will happen is that the POST will go to that URL. You will hit your first RewriteRule and get redirected to the contact form again, but since it's a browser redirect, it will probably just be a GET, so your POST content never makes it to your script.
Try changing the form action to http://accimpress.com/contact-us/ and see what happens.
It looks like CMS Made Simple doesn't support their Form Builder module once you've redirected your URL with Pretty URLs, so my solution for now was using Send This File, like so:
http://accimpress.com/upload/
But thanks for your suggestions Andrew!