2

I have a website composed as follows:

index.php page1.php page2.php page3.php - images image1.jpg image2.jpg - style style.css 

I want to write an htaccess file which can give me SEO friendly URL. In example:

  • https://example.com/page2.php should be https://example.com/page2

and also:

  • https://example.com/page2.php#mytab should be https://example.com/page2#mytab

But I would like to apply those rules only on the first directory, so "images" and "style" dirs can continue to be reached using the extensions.

Can somebody help? Thanks

2
  • Although you only appear to have .php files in the document root anyway? Commented Mar 7, 2022 at 19:29
  • yes exactly. I am even looking for rewrite URL 1 to 1. I mean I could even write a rule page by page. It does not matter to me. Commented Mar 7, 2022 at 19:34

1 Answer 1

3
  1. You should already be linking to the files without the .php extension on your internal URLs (ie. href="/page1", not href="/page1.php"). I'm also assuming that your URLs don't otherwise contain dots (which normally delimits the file extension).

  2. Implement a rewrite to append the .php extension if required. This needs to go near the top of the root .htaccess file:

    RewriteEngine On # Internally rewrite extenionless URLs to append ".php" if required # Tests only requests (that do not contain a dot) in the root directory RewriteCond %{DOCUMENT_ROOT}/$1.php -f RewriteRule ^([^./]+)$ $1.php [L] 

    Alternative for the RewriteCond (filesystem check):

    RewriteCond %{REQUEST_FILENAME}.php -f : 

    Alternatively, you could remove the RewriteCond directive altogether to unconditionally rewrite all requests in the root (without a file extension) to append the .php extension.

  3. (OPTIONAL) If you are changing a URL structure and removing .php from your URLs and the old URLs have been indexed by search engines and/or linked to by third parties then you need to also implement a redirect to remove the .php extension for SEO.

    Add the following immediately after the RewriteEngine directive above (before the internal rewrite):

    # Redirect to remove the `.php` extension inbound requests # Only affects the root directory RewriteCond %{ENV:REDIRECT_STATUS} ^$ RewriteRule ^([^./]+)\.php$ /$1 [R=301,L] 

    The condition that tests against the REDIRECT_STATUS environment variable ensures we don't redirect already rewritten requests by the later rewrite and this avoiding a redirect loop.

    NB: Test first with a 302 (temporary) redirect to avoid potential caching issues.

  4. Alternatively (instead of #3), to prevent direct access to the .php file and serve a 404 Not Found instead then add the following immediately after the RewriteEngine directive above (before the internal rewrite):

    # Prevent direct access to ".php" file and serve a 404 instead # Only affects the root directory RewriteCond %{ENV:REDIRECT_STATUS} ^$ RewriteRule ^([^./]+)\.php$ - [R=404] 

what is the best way to show the content of my custom 404 page every time a 404 error occurs? (I would not like to use redirect)

Use the following at the top of the .htaccess file, passing the full URL-path to the ErrorDocument directive.

ErrorDocument 404 /error-docs/e404.php 

The stated error document is called using an internal subrequest (there is no external redirect).

Note that this should include the .php file extension here - this is entirely invisible to the user.

Sign up to request clarification or add additional context in comments.

17 Comments

It seems to work with common name such as "index", but it doesn't if the files contain a dash symbol - like my-page.php is not reached by navigating example.com/my-page it gives me out a 404 not found error.
@Nino It shouldn't matter if the URL contains a dash (or hyphen). The regex ^([^./]+)$ only excludes dots and slashes (so only the first path segment is matched, excluding URLs that already contain a dot, ie. a file extension).
@Nino Do you have any other directives in the .htaccess file?
I saw it, but it is not working. I confirm that adding .php it works, and index (without .php) works.
I don't... only RewriteEngine On # Internally rewrite extenionless URLs to append ".php" if required # Tests only requests (that do not contain a dot) in the root directory RewriteCond %{DOCUMENT_ROOT}/$1.php -f RewriteRule ^([^./]+)$ $1.php [L]
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.