How to add some url rewrite to WHMCS? - apache

I'm trying to setup a classic .htaccess file for url rewriting.
For the moment I'm trying to simply rewrite login.php to login
I have tried using the following .htacess file :
RewriteEngine on
RewriteBase /
# Not Directory
RewriteCond %{REQUEST_FILENAME} !-d
# Not file
RewriteCond %{REQUEST_FILENAME} !-f
# Not Link ?? No Need.
#RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^login/$ login.php [L]
Which gives me the following when I try to access http://MYIP/login
http://MYIP/https://MYIP/login/
And I also tried using the following :
### BEGIN - WHMCS managed rules - DO NOT EDIT BETWEEN WHMCS MARKERS ###
<IfModule mod_rewrite.c>
RewriteEngine on
# RewriteBase is set to "/" so rules do not need updating if the
# installation directory is relocated. It is imperative that
# there is also a RewriteCond rule later that can effectively get
# the actual value by comparison against the request URI.
#
# If there are _any_ other RewriteBase directives in this file,
# the last entry will take precedence!
RewriteBase /
# Redirect directories to an address with slash
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+[^/])$ $1/ [R]
# Send all remaining (routable paths) through index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Determine and use the actual base
RewriteCond $0#%{REQUEST_URI} ([^#]*)#(.*)\1$
RewriteRule ^.*$ %2index.php [QSA,L]
</IfModule>
### END - WHMCS managed rules - DO NOT EDIT BETWEEN WHMCS MARKERS ###
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)([^/])$ /$1$2/ [L,R=301]
RewriteRule ^/login/$ ^/login.php$ [L]
And I got the same result.
Under the administration panel in WHMCS I set the url rewrite to Basic URLs.
Opening in private navigation I have error 404.

With your shown samples could you please try following. Please make sure you clear your browser cache before testing your URLs. Looks like you already had 1 Rule for non existing directories and non existing files so place your Login rule before it. I have put htaccess rule file here only from <IfModule mod_rewrite.c> to </IfModule> here.
### BEGIN - WHMCS managed rules - DO NOT EDIT BETWEEN WHMCS MARKERS ###
<IfModule mod_rewrite.c>
RewriteEngine on
# RewriteBase is set to "/" so rules do not need updating if the
# installation directory is relocated. It is imperative that
# there is also a RewriteCond rule later that can effectively get
# the actual value by comparison against the request URI.
#
# If there are _any_ other RewriteBase directives in this file,
# the last entry will take precedence!
RewriteBase /
# Redirect directories to an address with slash
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+[^/])$ $1/ [R]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^login/?$ login.php$ [NC,L]
# Send all remaining (routable paths) through index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Determine and use the actual base
RewriteCond $0#%{REQUEST_URI} ([^#]*)#(.*)\1$
RewriteRule ^.*$ %2index.php [QSA,L]
</IfModule>
### END - WHMCS managed rules - DO NOT EDIT BETWEEN WHMCS MARKERS ###

After some search and help from #RavinderSingh13, I get to the following working .htaccess :
### BEGIN - WHMCS managed rules - DO NOT EDIT BETWEEN WHMCS MARKERS ###
<IfModule mod_rewrite.c>
RewriteEngine on
# RewriteBase is set to "/" so rules do not need updating if the
# installation directory is relocated. It is imperative that
# there is also a RewriteCond rule later that can effectively get
# the actual value by comparison against the request URI.
#
# If there are _any_ other RewriteBase directives in this file,
# the last entry will take precedence!
RewriteBase /
# Redirect directories to an address with slash
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+[^/])$ $1/ [R]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
#Change
RewriteRule ^login$ ./login.php [L,NC]
# Send all remaining (routable paths) through index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Determine and use the actual base
RewriteCond $0#%{REQUEST_URI} ([^#]*)#(.*)\1$
RewriteRule ^.*$ %2index.php [QSA,L]
</IfModule>
### END - WHMCS managed rules - DO NOT EDIT BETWEEN WHMCS MARKERS ###

Related

Permanent redirect removal of file extensions in URL

When a page is visited using a URL that ends with .html, I'd like the URL to change to having no extension and report 301 permanently redirected. I'm having serious difficulty. After reading a lot of documentation and tutorials, and searching Stack Overflow for hours, the closest I've achieved is the opposite (URLs with no extension having one added) with this code:
<Location />
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteCond %{REQUEST_URI} !/$
RewriteCond %{REQUEST_URI} !\.html$
RewriteRule ^(.*)$ https://%{HTTP:Host}%{REQUEST_URI}.html [L,R=permanent]
</Location>
This can be done in two steps, with a REDIRECT_LOOP environment variable to prevent looping, with the directives in a Directory section for the web root (or .htaccess in the web root) so the matched string in the RewriteRule can be used for the permanent Redirect.
<Directory "/var/www/html">
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d # if the request is not a directory
RewriteCond %{REQUEST_FILENAME} !-f # if the request is not a file
RewriteCond %{REQUEST_FILENAME}\.html -f # if adding .html it is a file
RewriteCond %{REQUEST_URI} !\.html$ # if the request doesn't end in .html
RewriteCond %{REQUEST_URI} !/$ # if the request doesn't end in /
RewriteRule ^(.*)$ $1.html [L,E=LOOP:1] # then return the request as if it ended with .html and set the loop variable
RewriteCond %{ENV:REDIRECT_LOOP} !1 # if we didn't just added .html
RewriteRule ^(.*)\.html$ https://%{HTTP:Host}/$1 [L,R=permanent] # then 301 redirect the request to the request without the .html
</Directory>
This will make it so that if you have example.html and example/index.html then example.html will never be loaded.

Why is this .htaccess file routing static files to our front controller?

We have been using the following .htaccess file for years. We are using Apache 2.4.7. And for some reason, static files seem to be hitting our front controller index.php!
For example: https://example.com/apple-touch-icon.png is being handled by our front controller as we can see the response header includes X-Powered-By:PHP/5.5.9-1ubuntu4.3.
Can anyone spot the issue?
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
### Force SSL
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
### Canonicalize codeigniter URLs
# If your default controller is something other than
# "welcome" you should probably change this
RewriteRule ^(welcome(/index)?|index(\.php)?)/?$ / [L,R=301]
RewriteRule ^(.*)/index/?$ $1 [L,R=301]
# Removes trailing slashes (prevents SEO duplicate content issues)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ $1 [L,R=301]
# Enforce NO www
RewriteCond %{HTTP_HOST} ^www [NC]
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]
# Removes access to the system folder by users.
# Additionally this will allow you to create a System.php controller,
# previously this would not have been possible.
# 'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php/$1 [L]
# Checks to see if the user is attempting to access a valid file,
# such as an image or css document, if this isn't true it sends the
# request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
# Without mod_rewrite, route 404's to the front controller
ErrorDocument 404 /index.php
</IfModule>
It appears that your ErrorDocument directive is causing this (loading index.php when some resource i.e. image, js or css file is not found.
You can comment out this directive to avoid this behavior:
ErrorDocument 404 /index.php

Routing (htaccess) subfolder to file

I'm working on an add-on (for a CMS) to include news. To prevent creating many index-files I tried to use redirection with htaccess, but it seems to be complex for me ;-)
I'm using a .htaccess for the CMS in the root directory:
ErrorDocument 404 404.php
RewriteEngine On
# Redirect from http to https
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} !^domain.tld$ [NC]
RewriteRule ^(.*)$ https://domain.tld/$1 [L,R=301]
# If called directly - redirect to short url version
RewriteCond %{REQUEST_URI} !/page/intro.php
RewriteCond %{REQUEST_URI} /page
RewriteRule ^page/(.*)$ /$1 [L,R=301]
# Send the request to the index.php for processing
RewriteCond %{REQUEST_URI} !^/(page|backend|framework|include|languages|media|account|search|temp|templates/.*)$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([\/\sa-zA-Z0-9._-]+)$ /index.php?$1 [QSA,L]
# allow robots.txt (all other txt are denied before)
RewriteCond %{REQUEST_URI} !/robots\.txt$ [nocase]
RewriteRule \.txt$ - [forbidden,last]
The current structure:
--> page/
----> folder1/accessfile.php
----> folder1/.htaccess
I'd like to redirect from:
/page/folder1/accessfile/lorem/ipsum
and
/page/folder1/accessfile/lorem/ipsum/ #(folders that doesn't exist)
to:
/page/folder1/accessfile.php
I'd tried using this in page/folder1/.htaccess:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^accessfile/.*$ ./accessfile.php
But this doesn't work :-(
Put the htaccess file in your root directory alongside the page/ directory. If your htaccess file already resides there, put the rewrite rules before the # If called directly - redirect to short url version line.
Here's what you need:
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
# Make sure it's not an actula file/dir being accessed
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Make sure request uri does not contain the actual file
# name, avoiding recursive rewrite loops
RewriteCond %{REQUEST_URI} !accessfile\.php
RewriteRule page/folder1/accessfile/?(.*)? /page/folder1/accessfile.php?q=$1 [R,L,QSA]
</IfModule>
## Results
# page/folder1/accessfile => page/folder1/accessfile.php?q=
# page/folder1/accessfile/foo => page/folder1/accessfile.php?q=foo
# page/folder1/accessfile/foo/bar => page/folder1/accessfile.php?q=foo/bar
# page/folder1/accessfile/foo/bar/baz => page/folder1/accessfile.php?q=foo/bar/baz
As you see, for convenience, the rewrite rule rewrites the request URI as query string parameters. So the PHP file has access to the passed data using $_GET['q'].
If you wish to preserve the clean URL, and rewrite the request to the php file under the hood, drop that R flag.

The .htaccess redirect unwantedly

I want that when domain.ext/sitemap.xml is called, domain.ext/sitemap.xml.php is opened. This I got working.
My problem is that the browser redirects to the php file, I dont want that, it should keep saying sitemap.xml
# Catch sitemap
RewriteCond %{REQUEST_URI} ^/sitemap.xml$
#RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.*) http://%{HTTP_HOST}/sitemap.xml.php [L]
## Internally rewrite extensionless file requests to .php files ##
# If the requested URI does not contain a period in the final path-part
RewriteCond %{REQUEST_URI} !(\.[^./]+)$
# test if sitemap
RewriteCond %{REQUEST_URI} !^/sitemap
# and if it does not exist as a directory
RewriteCond %{REQUEST_FILENAME} !-d
# and if it does not exist as a file
RewriteCond %{REQUEST_FILENAME} !-f
# then add .php to get the actual filename
RewriteRule ^(.*)/? index.php?q=$1 [L]
Anyone who can tell me why?
Remove http:// from target URI to enable internal rewriting:
# Catch sitemap
RewriteRule ^sitemap\.xml$ /sitemap.xml.php [L]
## Internally rewrite extensionless file requests to .php files ##
# If the requested URI does not contain a period in the final path-part
RewriteCond %{REQUEST_URI} !(\.[^./]+)$
# test if sitemap
RewriteCond %{REQUEST_URI} !^/sitemap
# and if it does not exist as a directory
RewriteCond %{REQUEST_FILENAME} !-d
# and if it does not exist as a file
RewriteCond %{REQUEST_FILENAME} !-f
# then add .php to get the actual filename
RewriteRule ^(.+?)/?$ index.php?q=$1 [L]
PS: If target URI starts with http:// then mod_rewrite redirects with R=302 status.

htaccess remove index.php from url

I have a problem whereby google has indexed some pages with the wrong url.
The url they are indexing is:
http://www.example.com/index.php/section1/section2
I need it to redirect to:
http://www.example.com/section1/section2
.htaccess isn't my forte, so any help would be much appreciated.
The original answer is actually correct, but lacks explanation. I would like to add some explanations and modifications.
I suggest reading this short introduction https://httpd.apache.org/docs/2.4/rewrite/intro.html (15mins) and reference these 2 pages while reading.
https://httpd.apache.org/docs/2.4/mod/mod_rewrite.html
https://httpd.apache.org/docs/2.4/rewrite/flags.html
This is the basic rule to hide index.php from the URL. Put this in your root .htaccess file.
mod_rewrite must be enabled with PHP and this will work for the PHP version higher than 5.2.6.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /index.php/$1 [L]
Think %{REQUEST_FILENAME} as the the path after host.
E.g. https://www.example.com/index.html, %{REQUEST_FILENAME} is /index.html
So the last 3 lines means, if it's not a regular file !-f and not a directory !-d, then do the RewriteRule.
As for RewriteRule formats:
So RewriteRule (.*) /index.php/$1 [L] means, if the 2 RewriteCond are satisfied, it (.*) would match everything after the hostname. . matches any single character , .* matches any characters and (.*) makes this a variables can be references with $1, then replace with /index.php/$1. The final effect is to add a preceding index.php to the whole URL path.
E.g. for https://www.example.com/hello, it would produce, https://www.example.com/index.php/hello internally.
Another key problem is that this indeed solve the question. Internally, (I guess) it always need https://www.example.com/index.php/hello, but with rewriting, you could visit the site without index.php, apache adds that for you internally.
Btw, making an extra .htaccess file is not very recommended by the Apache doc.
Rewriting is typically configured in the main server configuration
setting (outside any <Directory> section) or inside <VirtualHost>
containers. This is the easiest way to do rewriting and is recommended
To remove index.php from the URL, and to redirect the visitor to the non-index.php version of the page:
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
This will cleanly redirect /index.php/myblog to simply /myblog.
Using a 301 redirect will preserve Google search engine rankings.
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)index\.php($|\ |\?)
RewriteRule ^ /%1 [R=301,L]
Assuming the existent url is
http://example.com/index.php/foo/bar
and we want to convert it into
http://example.com/foo/bar
You can use the following rule :
RewriteEngine on
#1) redirect the client from "/index.php/foo/bar" to "/foo/bar"
RewriteCond %{THE_REQUEST} /index\.php/(.+)\sHTTP [NC]
RewriteRule ^ /%1 [NE,L,R]
#2)internally map "/foo/bar" to "/index.php/foo/bar"
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ /index.php/$1 [L]
In the spep #1 we first match against the request string and capture everything after the /index.php/ and the captured value is saved in %1 var. We then send the browser to a new url.
The #2 processes the request internally. When the browser arrives at /foo/bar , #2rule rewrites the new url to the orignal location.
Steps to remove index.php from url for your wordpress website.
Check you should have mod_rewrite enabled at your server.
To check whether it's enabled or not - Create 1 file phpinfo.php at your root folder with below command.
<?php
phpinfo?();
?>
Now run this file - www.yoursite.com/phpinfo.php and it will show mod_rewrite at Load modules section.
If not enabled then perform below commands at your terminal.
sudo a2enmod rewrite
sudo service apache2 restart
Make sure your .htaccess is existing in your WordPress root folder, if not create one .htaccess file
Paste this code at your .htaccess file :-
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Further make permission of .htaccess to 666 so that it become writable and now you can do changes in your wordpress permalinks.
Now go to Settings -> permalinks -> and change to your needed url format.
Remove this code /index.php/%year%/%monthnum%/%day%/%postname%/
and insert this code on Custom Structure: /%postname%/
If still not succeeded then check your hosting, mine was digitalocean server, so I cleared it myself
Edited the file /etc/apache2/sites-enabled/000-default.conf
Added this line after DocumentRoot /var/www/html
<Directory /var/www/html>
AllowOverride All
</Directory>
Restart your apache server
Note: /var/www/html will be your document root
Do the following steps
1. Make sure that the hosting / your pc mod_rewrite module is active. if not active then try to activate in a way, open the httpd.conf file. You can check this in the phpinfo.php to find out.
change this setting :
#LoadModule rewrite_module modules/mod_rewrite.so
to be and restart wamp
LoadModule rewrite_module modules/mod_rewrite.so
2. Then go to .htaccess file, and try to modify to be:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\?*$ index.php/$1 [L,QSA]
if above does not work try with this:
RewriteEngine on
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php
3. Move .htaccess file to root directory, where is index.php there.
www OR root folder
- index.php
- .htaccess
Some may get a 403 with the method listed above using mod_rewrite. Another solution to rewite index.php out is as follows:
<IfModule mod_rewrite.c>
RewriteEngine On
# Put your installation directory here:
RewriteBase /
# Do not enable rewriting for files or directories that exist
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
I have used many codes from the above mentioned sections for removing index.php form the base url. But it was not working from my end. So, you can use this code which I have used and its working properly.
If you really need to remove index.php from the base URL then just put this code in your htaccess.
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
This will work, use the following code in .htaccess file
RewriteEngine On
# Send would-be 404 requests to Craft
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/(favicon\.ico|apple-touch-icon.*\.png)$ [NC]
RewriteRule (.+) index.php?p=$1 [QSA,L]
I don't have to many bulky code to give out just a little snippet solved the issue for me.
i have https://example.com/entitlements/index.php rather i want anyone that types it to get error on request event if you type https://example.com/entitlements/index
you will still get error since there's this word "index" is contained there will always be an error thrown back though the content of index.php will still be displayed properly
cletus post on "https://stackoverflow.com/a/1055655/12192635" which
solved it
Edit your .htaccess file with the below
to redirect people visiting https://example.com/entitlements/index.php to 404 page
RewriteCond %{THE_REQUEST} \.php[\ /?].*HTTP/
RewriteRule ^.*$ - [R=404,L]
to redirect people visiting https://example.com/entitlements/index to 404 page
RewriteCond %{THE_REQUEST} \index[\ /?].*HTTP/
RewriteRule ^.*$ - [R=404,L]
Not withstanding we have already known that the above code works with already existing codes on stack see where i applied the code above just below the all codes at it end.
# The following will allow you to use URLs such as the following:
#
# example.com/anything
# example.com/anything/
#
# Which will actually serve files such as the following:
#
# example.com/anything.html
# example.com/anything.php
#
# But *only if they exist*, otherwise it will report the usual 404 error.
Options +FollowSymLinks
RewriteEngine On
# Remove trailing slashes.
# e.g. example.com/foo/ will redirect to example.com/foo
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ /$1 [R=permanent,QSA]
# Redirect to HTML if it exists.
# e.g. example.com/foo will display the contents of example.com/foo.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)$ $1.html [L,QSA]
# Redirect to PHP if it exists.
# e.g. example.com/foo will display the contents of example.com/foo.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ $1.php [L,QSA]
RewriteCond %{THE_REQUEST} \.php[\ /?].*HTTP/
RewriteRule ^.*$ - [R=404,L]
RewriteCond %{THE_REQUEST} \index[\ /?].*HTTP/
RewriteRule ^.*$ - [R=404,L]
try this, it work for me
<IfModule mod_rewrite.c>
# Enable Rewrite Engine
# ------------------------------
RewriteEngine On
RewriteBase /
# Redirect index.php Requests
# ------------------------------
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteCond %{THE_REQUEST} !/system/.*
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,L]
# Standard ExpressionEngine Rewrite
# ------------------------------
RewriteCond $1 !\.(css|js|gif|jpe?g|png) [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
For more detail
create .htaccess file on project root directory and put below code for remove index.php
RewriteEngine on
RewriteCond $1 !^(index.php|resources|robots.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]