How to add trailing slash to end of URL - php

This is my .htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
RewriteRule !\.(css|js|ico|zip|rar|png|jpg|gif|pdf)$ index.php [L]
AddType application/x-httpd-php .php .phtml
and I need to know how to add trailing slash to end of URL when I have this code:
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
for add www in .htaccess
For example URL without code for this:
www.example.com/introduction
and with this code:
www.example.com/introduction/

Have it this way:
AddType application/x-httpd-php .php .phtml
RewriteEngine On
RewriteBase /
# add www
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
# add a trailing slash to non-files
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule !\.(css|js|ico|zip|rar|png|jpe?g|gif|pdf)$ index.php [L,NC]

RULE FOR IIS USERS : ADDING / TO All URL. Worked for all seo
<rule name="Add trailing slash" stopProcessing="true">
<match url="^(.*)([^/])$" />
<conditions logicalGrouping="MatchAny">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" pattern="(.*?)\.[a-zA-Z]{1,4}$" negate="true" />
</conditions>
<action type="Redirect" url="{R:0}/" redirectType="Permanent" />
</rule>

Related

.htaccess to web config conversion

can anyone help me to convert this .htaccess file to web.config IIS. I have tried with some online tools also but still not working.
The server is windows 10and IIS version 7
Options +FollowSymlinks
RewriteEngine on
# deal with admin first
RewriteCond %{REQUEST_URI} ^/<foldername>/(admin)
RewriteCond %{REQUEST_URI} !^/<foldername>/backend/web/(assets|css)/
RewriteCond %{REQUEST_URI} !^/<foldername>/backend/web/(assets|js)/
RewriteRule ^admin/assets/(.*)$ backend/web/assets/$1 [L]
RewriteCond %{REQUEST_URI} ^/<foldername>/(admin)
RewriteRule ^.*$ backend/web/index.php [L]
RewriteCond %{REQUEST_URI} ^/<foldername>/(assets|css)
RewriteCond %{REQUEST_URI} ^/<foldername>/(assets|js)
RewriteRule ^assets/(.*)$ frontend/web/assets/$1 [L]
RewriteCond %{REQUEST_URI} !^/<foldername>/(frontend|backend)/web/(assets|css)/
RewriteCond %{REQUEST_URI} !^/<foldername>/(frontend|backend)/web/(assets|js)/
RewriteCond %{REQUEST_URI} !^/<foldername>/download/
RewriteCond %{REQUEST_URI} !^/<foldername>/fonts/
RewriteCond %{REQUEST_URI} !index.php
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ frontend/web/index.php
Could you help me to fix the issue?
You can try below rules:
<rule name="rule1" stopProcessing="true">
<match url="^admin/assets/(.*)$" />
<action type="Rewrite" url="/backend/web/assets/{R:1}" />
</rule>
<rule name="rule2" stopProcessing="true">
<match url="^.*$" />
<action type="Rewrite" url="/backend/web/index.php" />
</rule>
<rule name="rule3" stopProcessing="true">
<match url="^assets/(.*)$" />
<action type="Rewrite" url="/frontend/web/assets/{R:1}" />
</rule>
<rule name="rule4">
<match url="^.*$" />
<action type="Rewrite" url="/frontend/web/index.php" />
</rule>
And here is a microsoft article on the equivalent components: Translate .htaccess Content to IIS web.config.

Web.config: redirect URLs entered with trailing slash to URL without trailing slash

I have inherited a website PHP website, running on IIS 7.5.
If I go to:
https://www.example.com/page there are no errors, it displays page.php content as expected.
If I go to:
https://www.example.com/page/ I receive a 404 not found error. I'd like this to redirect /page and display page.php content.
I believe it's because the server is looking for a file page/.php (or page.php/ ??), but I need some help with what code to place in web.config to fix this.
If you use iis then add this in web.config To always remove trailing slash from the URL:
<rule name="Remove trailing slash" stopProcessing="true">
<match url="(.*)/$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Redirect" redirectType="Permanent" url="{R:1}" />
</rule>
if you have an Apache Web Server then In .htaccess add this :
RewriteEngine on
#unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [R=301,L]
#resolve .php file for extensionless php urls
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
#redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*[^.#?\ ]+\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(([^/]+/)*[^.]+)\.php /$1 [R=301,L]

block .php .html extension

I am using a Godaddy domain and i want to block/get rid the extension of my files for example: www.example.com/index.php to www.example.com/index
I am aware of .htaccess so here is my RewriteRule code
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
# Add trailing slash to url
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/|#(.*))$
RewriteRule ^(.*)$ $1/ [R=301,L]
# Remove .php-extension from url
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^\.]+)/$ $1.php
# Remove .html-extension from url
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^\.]+)/$ $1.html
</IfModule>
someone help me please.
On Apache
Because you're on GoDaddy, you need to enable MultiViews in your .htaccess file.
Add the following code:
Options +MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Should work. Reference: https://alexcican.com/post/how-to-remove-php-html-htm-extensions-with-htaccess/
On IIS/Windows
IIS uses a web.config file instead of a .htaccess file. Create a file web.config in the root directory (or which every directory you want the rules to apply), and put the following code in it
<rewrite>
<rule name="hide .php extension" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="{R:1}.php" />
</rule>
</rewrite>
Reference: https://www.saotn.org/iis-url-rewrite-hide-php-extension/

URL redirects in htaccess

I have a htaccess file that is set and running on a live server. Everything is functioning fine with it but I have to add some specific URL re-directs to it. The specific re-directs had been setup in an IIS web.config file. An example:
<rule name="URL re-direct" stopProcessing="true">
<match url="^program_consumer((\.cfm)|(\.php)|()+)((\?.*)|())$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
<action type="Redirect" url="site_programs{R:7}" />
</rule>
I'm having a hard time translating this to mt htaccess file. I've tried:
AddDefaultCharset OFF
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
# Resolve .php file for extensionless php urls
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
# Re-direct Non-file Non-directories
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([program_consumer]+)((\.cfm)|(\.php)|()+)((\?.*)|())$ http://{HTTP_HOST}/site_programs$1 [L,R=301]
Obviously, the program_consumer is not a file that exists anywhere in the root document directory, but the site_programs does exist and is served as an extension-less PHP file. Any thoughts on what I'm missing?
Your regex in RewriteRule seems faulty and it needs to be placed before internal rewrite rule:
AddDefaultCharset OFF
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^(program_consumer\.(?:cfm|php))$ /site_programs/$1 [L,R=301,NC,NE]
# Resolve .php file for extensionless php urls
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

remove index.php not working with my live server in Code Igniter

remove index.php not working with my live server
it's work in my local server
I have Tried Below code
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
It's work in local wamp server but not in live server
Config.php detail
$config['base_url'] = 'http://ec2-174-129-44-226.compute-1.amazonaws.com/';
$config['index_page'] = '';
$config['uri_protocol'] = 'AUTO';
$config['url_suffix'] = '';
$config['language'] = 'english';
I've posted this answer dozens of time (2) there is a link in comments to a question simmilar to yours.
Please go to config/config.php and find this variable
$config['index_page'] = 'index.php' make sure it looks like this $config['index_page'] = '';
in the same file find $config['base_url'] = ''; make sure it looks like this
$config['base_url'] = 'http://ec2-174-129-44-226.compute-1.amazonaws.com/';
Also in core folder (where system, application is) create .htaccess file and make sure it looks like this
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#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]
#When your application folder isn't in the system folder
#This snippet prevents user access to the application folder
#Submitted by: Fabdrol
#Rename 'application' to your applications folder name.
RewriteCond %{REQUEST_URI} ^application.*
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>
# If we don't have mod_rewrite installed, all 404's
# can be sent to index.php, and everything works as normal.
# Submitted by: ElliotHaughin
ErrorDocument 404 /index.php
</IfModule>
You can change your .htaccess like this
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
and change the value of "AllowOverride None" on /etc/httpd/conf/httpd.conf (if you use AMI Linux)
into "AllowOverride All"
after that restart your apache with command like this:
sudo service httpd restart
<?xml version="1.0"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Main Rule" stopProcessing="true">
<match url=".*" /> <conditions logicalGrouping="MatchAll"> <add input="REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" /> </conditions> <action type="Rewrite" url="index.php" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

Categories