Apache adds parameters to meta refresh url - .htaccess

After redirecting a page with
<meta http-equiv="refresh" content="5;URL=https://myserver.com/category/item.php">
the location in the browser shows https://myserver.com/category/item.php?category=item !
I'd like to remove this "?category=item" from the url.
.htaccess rewrite rule is :
RewriteRule ^category/(.*).php index.php?category=$1 [L]
This issue only happens on the public shared server, not on my local Apache installation, and only after a refresh, despite the fact that the php script and the .htaccess rules are exactly the same on both servers. 3 RewriteCond about SSL are automatically added to the remote .htaccess, though :
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/\.well-known/acme-challenge/.+$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
Could this be the problem ? I have the same issue with any pages (and differents rewrite rules) when I use meta refresh on this remote server.
Is there any way to prevent the server to append this parameter to the original url ?
Edit : I tried to add a [QSD] flag to the rewrite_rule, or a ? after the index.php?category=$1, and contacted the help-desk of the web-hosting company Funio, to no avail.

Related

Redirect all subdomains and subdirectories to index page using .htaccess

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.

How to not redirect Twitterbot when using cloudflare SSL?

Twitter Cards (twitter:image or og:image) won’t allow https from a shared SSL including Cloudflare. The workaround mentioned is to create an .htaccess file and add code to not redirect twitterbot to the shared https. The problem is this is breaking my pages saying there are too many redirects.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^example.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteCond %{HTTP_USER_AGENT} !Twitterbot [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301,NC]
</IfModule>
That being said, when I run the page through Twitter Card Validator when I have that .htaccess code, the image does show up fine on the Twitter Card Validator. But as I mentioned, the website breaks because of too many redirects.
I tried removing the .htaccess code and putting the twitter card image in a directory called /social/ and putting this page rule in Cloudflare: example.com/social/ - Disable Security as the first rule (only one rule works per page), and this simply is ignored by twitterbot even though if i manually put the path to the image in that directory it’s correctly not redirecting to https.
<meta property="og:image" content="http://example.com/social/image.jpg">
<meta name="twitter:image" content="http://example.com/social/image.jpg">
When validating the page on Twitter Card Validator and using ?utm_source=a-random-number to make sure it's pulling a fresh page, it pulls all the info correctly except for the image. Facebook pulls the image fine, it's just twitterbot's incompatibility with Cloudflare's flexible shared SSL. I'm not on WordPress and this is on Apache. There is currently no .htaccess file. Any ideas?
=============
EDIT
Ok, got it working. The solution of adding:
example.com/social/ - Disable Security
on Cloudflare's SSL page rules was correct, but I also had to include two meta og parameters on the head:
<meta property="og:image:width" content="1200" />
<meta property="og:image:height" content="630" />
without that the image wouldn't show on Twitter Card Validator, but does when you add it. Hope that helps someone else.
The redirect loop isn't caused by the Twitterbot exception (that would simply prevent the rule being executed), but because you are checking the SERVER_PORT. When using "Cloudflare's flexible shared SSL" your application server is always communicating on port 80 (ie. HTTP). Cloudflare is an SSL proxy. The connection from the client to Cloudflare is secure, but the connection from Cloudflare to your application server is not. Reference: https://support.cloudflare.com/hc/en-us/articles/200170416#h_4e0d1a7c-eb71-4204-9e22-9d3ef9ef7fef
Instead of checking the SERVER_PORT in order to determine whether the client is not yet on an HTTPS connection, you should check the X-Forwarded-Proto HTTP request header instead (set by the Cloudflare proxy). Reference: https://support.cloudflare.com/hc/en-us/articles/200170986-How-does-Cloudflare-handle-HTTP-Request-headers-
For example:
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} !https [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com [NC]
RewriteCond %{HTTP_USER_AGENT} !twitterbot [NC]
RewriteRule (.*) https://www.example.com/$1 [R=301,L]
I've combined your two conditions that check the hostname (also avoids the need for the OR flag). Unless you are serving multiple domains/subdomains/hostnames from this account then that condition is not required at all.
You will need to clear your browser cache before testing. (Test with 302 - temporary - redirects to avoid caching issues.)

Codeigniter project - trying to view the index page (main page in application) using XAMPP

In XAMPP and my current setup I can simply type in https://www.localhost:1234/testsite, and it will automatically load the index.php page in the directory testsite, and display it on the screen.
With codeigniter, I understand the routing is more complex, and I don't know what to do, as a complete beginner, to view the main index page.
The index page that I want to be the home (i.e loaded on typing in https://www.localhost:1234/public_html) is located in C:\xampp\htdocs\public_html\application\views\home\index.php
Can someone please suggest what I need to do to get this working?
I have tried this: https://www.localhost:1234/public_html/application/views/home .but it doesn't work
The .htaccess file, and yes this is a fully fledged hosted site, is as below: What do I need to change here, or can I delete it for working on localhost?
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{REQUEST_URI} !^/\.well-known/acme-challenge/[0-9a-zA-Z_-]+$
RewriteCond %{REQUEST_URI} !^/\.well-known/cpanel-dcv/[0-9a-zA-Z_-]+$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
# force ssl
RewriteCond %{SERVER_PORT} ^80$
RewriteCond %{REQUEST_URI} !^/\.well-known/acme-challenge/[0-9a-zA-Z_-]+$
RewriteCond %{REQUEST_URI} !^/\.well-known/cpanel-dcv/[0-9a-zA-Z_-]+$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]
What do I need to type in the URL, or change in the .htaccess file, or somewhere else to simply load the index file as shown above?
I assume a change needs to be made to these two lines, but I'm not sure what?
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
would the https:///www.%HTTP_HOST} be changed to localhost:1234?
Also, I'm aware of the format as described on the codeigniter website:
https://www.localhost:1234/public_html/home/index/index.php
doesn't work either,
where home is the controller (in application) and index is the function, see below:
class Home extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('homes');
}
public function index()
{
//$key = $config['encryption_key'];
$this->load->view('template/header.php');
$this->load->view('home/index.php');
$this->load->view('template/footer.php');
}
I also tried localhost:1234/index.php/home/index as suggested below, to no avail.
Based on your .htaccess I'm assuming this is a fully developed site and not just a blank CodeIgniter install. As such most of your .htaccess rules reference a remote or live site.
I cannot speak for your current setup, but basically by default with no .htaccess CI works as follows.
If you have a controller in application/controllers/ called Test.php and a sub function called some_method then the url is localhost/index.php/test/some_method
In default CodeIgniter installs the index is at localhost/index.php or localhost/index.php/index. The welcome controller you see in default installs actually references controllers/welcome yet although you can access it via localhost/index.php/welcome you can also access it via localhost/index.php and this is where routes/default controllers come in to play. Explaining routes in depth is a bit outside of the scope of stack, but you can find more information here: https://www.codeigniter.com/user_guide/general/routing.html
Being new to CI I can say that the documentation is quite good and you should take your time to understand the architecture before going forward.
If you wish to remove index.php and just see localhost/test/some_method you need some .htaccess rules https://www.codeigniter.com/userguide3/general/urls.html#removing-the-index-php-file

htaccess redirect, everything is redirecting instead of specific requests

I'm trying to setup a new project structure. Due to some limitations of my cloud provider, I need to do some htaccess magic which I'm struggling with.
The project structure is as following
/docroot => Contains my Drupal installation
/docroot/frontend => Contains an Angular frontend
/docroot/pim => Is a symlink to /docroot.
What we need is that when we surf to example.com/pim that it redirects to the Drupal docroot /docroot. Since due to limitations of the cloud provider we can't put it in the /pim folder, they suggested to make a symlink.
Any other request to example.com should go to docroot/frontend.
So I've added the following code in my htaccess file
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/frontend.*$
RewriteRule !^pim($|/) http://example.com/frontend%{REQUEST_URI} [L,R=302]
Current behaviour is that all requests go to /frontend/index.php (I think the index.php comes from the .htaccess of Drupal, so the redirect is not done cleanely) While I would expect a request to example.com/test to direct to example.com/frontend/test
Also example.com/pim points to example.com/frontend/index.php instead of staying in the /pim directory
I've been able to fix it. :)
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/frontend.*$
RewriteCond %{REQUEST_URI} !^/pim.*$
RewriteRule ^ frontend%{REQUEST_URI} [L,R=302]
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/frontend.*$
RewriteRule ^ %{REQUEST_URI} [L]

htaccess on one.com webspace

Recently I moved my websites to the hoster one.com. They have setup an automated mechanism (I dunno what they use to achieve that) to rewrite any first-level folder on the webspace to a subdomain.
I.e. the folder http://example.com/folder1/ will be also available as http://folder1.example.com/
Now, I have a site, that is using quite a lot javascript to include pages from a hardcoded, static source. Due to the SOP the scripts are working depending on which hardcoded reference they use.
So, to make sure that everybody gets a working version of the website, i wanted to redirect the direct folder access to the subdomain as well.
My htaccess for this - which is working localy and on various htaccess-testers out there - seems to be not working with one.com:
RewriteEngine On
#Rewrite Access to folder1-folder to subdomain.
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteCond %{REQUEST_URI} ^/folder1.*?$ [NC]
RewriteRule .* http://folder1.example.com/ [R=301,L]
Since I don't know the exact mechanism one.com is using to achieve the mentioned behaviour it might just be a conflict with my rules.
Support says, that all the used commands are fully supported, and therefore wasn't be able to tell what's going wrong...
Does anybody have encountered something similiar and has a hint for me?
just fiured out the solution:
RewriteEngine On #does not work
vs.
RewriteEngine on #does work
You need to check that the actual request was made for /folder/ and not the URI (which can internally be rewritten). Try:
#Rewrite Access to folder1-folder to subdomain.
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /+folder1/ [NC]
RewriteRule ^folder1/(.*)$ http://folder1.example.com/$1 [R=301,L]

Resources