1

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.

Erin M.
  • 9
  • 3

1 Answers1

1

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]
Mohammad
  • 1,549
  • 1
  • 15
  • 27