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.

1 Answer 1

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]
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your quick reply! It is IIS. This has worked for me. Much appreciated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.