RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (?!^infos/)^(.+?)/?$ /dl.php?f=$1 [L,QSA,NC]
This rule stopped working after switching to https / SSL with Cloudflare and i can't figure why ?
If i try the exact same URL's without the https:// part, the pages are accessibles, so the rule is working but only for non-secured URL's
Remember that the http and the https variants of a domain are served by two completely separate hosts from the point of view of the http server. That means you have separate configurations. Most likely the ssl host does not have enabled the interpretation of dynamic configuration files (".htaccess"). Take a look at the apache AllowOverride directive...
And a general hint: you should always prefer to place such rules inside the http servers (virtual) host configuration instead of using dynamic configuration files (.htaccess style files). Those files are notoriously error prone, hard to debug and they really slow down the server. They are only provided as a last option for situations where you do not have control over the host configuration (read: really cheap hosting service providers) or if you have an application that relies on writing its own rewrite rules (which is an obvious security nightmare).
Related
Quick summary, I have implemented the following .htaccess file which successfully redirects http:// and any www. searches to https://
My issue - After redirectrule has been applied it then leaves a trailing //? so for example: http://www.example.com becomes https://example.com//?
Another example of another page: http://www.example.com/test becomes https://example.com//test?
So to clarify further. I am happy with the http to https redirect however I only need one final trailing slash and nothing else to my URL, any help and advice would be great as I cannot for the life of me find any other example like this.
Required - http://www.example.com to become https://example.com/
Here is my .htaccess code...
RewriteEngine On
RewriteCond %{SERVER_PORT} !=443
RewriteRule ^(.*)$ https://settlerslodge.co.uk/$1 [R,L]
Use below rewrite rule and test.
RewriteEngine On
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^/?(.+)/?$ https://%{HTTP_HOST}/$1 [L,R]
This would be a clean setup:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.+)/?$ https://example.com/$1 [R=301,QSD,END]
It is a good idea to start out with a 302 temporary redirection and only change that to a 301 permanent redirection later, once you are certain everything is correctly set up. That prevents caching issues while trying things out...
In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server. You will see a definite hint to an unsupported [END] flag in your http servers error log file in that case. You can either try to upgrade or use the older [L] flag, it probably will work the same in this situation, though that depends a bit on your setup.
This implementation will work likewise in the http servers host configuration or inside a distributed configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a distributed configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using distributed configuration files (".htaccess"). Those distributed configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).
my problem is that in root directory I have this rewrite rule in my .htaccess file
RewriteRule main-page /index.php
and it works when I go to the root directory in browser.
But when I visit page.com/panel/main-page it also redirects to the index.php in root directory.
You need to be more precise in your matching pattern. Your current matching pattern main-page will match any path in this list of examples, which is not what you actually want:
/main-page
/folder/main-page
/folder/main-page/whatever
/some-other-main-page
/some-other-main-page-in-green
Instead you want to be precise and only match the literal string "main-page" as absolute path and nothing else:
RewriteEngine on
RewriteRule ^/?main-page$ /index.php [END]
In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server. You will see a definite hint to an unsupported [END] flag in your http servers error log file in that case. You can either try to upgrade or use the older [L] flag, it probably will work the same in this situation, though that depends a bit on your setup.
This implementation will work likewise in the http servers host configuration or inside a distributed configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a distributed configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using distributed configuration files (".htaccess"). Those distributed configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).
Good question.
That line is correct:
RewriteRule main-page /index.php
But in that case, we've got if URL contains main-page it always be redirected to the /index.php.
In pure apache2 configuration, below lines always do what you want:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^main-page /index.php
RewriteRule ^(*.)/main-page $1/index.php
I'm new to htaccess and I need some help
how do I change the url automatically from from .htaccses
if I write url like:
url/index.php/pages
url will change automatic to:
url/pages
Thanks in advance.
This appears to be pretty straight forward, you would have found hundreds of existing answers and examples alone here on StackOberflow...
RewriteEngine on
RewriteRule ^/?index\.php/pages/?$ /pages [R=301]
This assumes that in your question, in the given path url/index.php/pages the "url" refers to a prefix of protocol scheme and host name, so would usually be written as https://example.com/index.php/pages...
It is a good idea to start out with a 302 temporary redirection and only change that to a 301 permanent redirection later, once you are certain everything is correctly set up. That prevents caching issues while trying things out...
I dare say however that you also need the corresponding internal rewrite to again be able to process such redirected requests. Adding that the example looks like this:
RewriteEngine on
RewriteRule ^/?index\.php/pages/?$ /pages [R=301]
RewriteRule ^/?pages/?$ /index.php/pages [END]
In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server. You will see a definite hint to an unsupported [END] flag in your http servers error log file in that case. You can either try to upgrade or use the older [L] flag, it probably will work the same in this situation, though that depends a bit on your setup.
This get more complex if your question does not only target the single, specific path /index.php/pages but actually any "pages" to follow in the path after the leading /index.php/. For that you'd need something a bit more complex:
RewriteEngine on
RewriteRule ^/?index\.php/(.*)$ /$1 [R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/index\.php/
RewriteRule ^/?(.*)$ /index.php/$1 [END]
This implementation will work likewise in the http servers host configuration or inside a dynamic configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a dynamic configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).
Here's my code in .htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([0-9]+)/$ index.index?pm=$1 [L]
It doesnt work using this link:
http://localhost/display/1001
but it does when you add ? before the number
http://localhost/display/?1001
Your question is unclear, you did not provide enough information about your actual situation and what you are trying to achieve. So we have to do a bit of guess work here and hope that this is what you are looking for:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?display/(\d+)/?$ /index.php?pm=$1 [END,QSA]
In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server. You will see a definite hint to an unsupported [END] flag in your http servers error log file in that case. You can either try to upgrade or use the older [L] flag, it probably will work the same in this situation, though that depends a bit on your setup.
This rule will work likewise in the http servers host configuration or inside a dynamic configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a dynamic configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).
I would like to redirect any non-existent requests (files\folders\subfolders) to the root domain.com. Currently it's showing:
Not Found
The requested URL /awddawd was not found on this server.
Apache/x.x.x.x.x (Debian) Server at domain.com Port 80
You can use basic rewriting as offered by Apaches mod_rewrite module:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule - / [L,R=301]
Best is to place such rules right into the (virtual) host configuration between basic host definitions and directory/location tags.
An alternative is to use .htaccess style files in the document file hierarchy, but that is much more error prone, harder to debug and really slows the server down. So only do this if you have no access to the server configuration. Such files must be readable by the http server process and obviously support for such files must be enabled in the server configuration. A .htaccess style file holding general rules like this probably is best placed right inside the top level directory of the (virtual) hosts DocumentRoot.