Hi
I recently added
Options +Multiviews

to my .htaccess file so that URLs on my site would be

mydomain.com/features/cars

rather than

mydomain/features/cars.php

It's working fine, but I now wish to redirect bookmarked users and robots that request the old URLs.

I've tried

Redirect /features/cars.php
http://mydomain.com/features/cars

but this gives file not found: presumably as you can't redirect to the same place.
Does anyone know how to do this?

thanks in advance

Dani AI

Generated

As noted, enabling Apache content-negotiation (MultiViews) lets a request like /dir/name serve the underlying /dir/name.php. To avoid duplicate-content and bookmark problems, it’s best to externally redirect any client request that explicitly asks for *.php to the extensionless canonical URL, and separately let extensionless requests be internally mapped to the .php file only when that file exists.

A compact, safe two-step .htaccess approach (external redirect then internal rewrite) looks like this:

RewriteEngine On

# externally redirect client requests for /something.php -> /something
RewriteCond %{THE_REQUEST} \s/([^\s?]+)\.php[^\s]*\s [NC]
RewriteRule ^(.+)\.php$ /$1 [R=301,L]

# internally serve /something by /something.php only if that .php file exists
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]

The key is checking the original client request (THE_REQUEST) before issuing the 301 — that prevents loops when the server internally maps /name to name.php. The second rule guards against turning every arbitrary path into a .php lookup by only rewriting when filename.php actually exists. For the two-rule pattern and variations, see the canonical examples and discussion. (stackoverflow.com)

Notes and troubleshooting: preserve query strings if needed (use the rewrite flags or rely on mod_rewrite’s query handling), test redirects with a tool that shows headers (for example curl -I), and test with temporary redirects first (302) because 301s are aggressively cached by browsers and crawlers. If mod_rewrite is unavailable on a host, implement a server-side 301 from the .php entry scripts instead. For details on THE_REQUEST, flags and query-string behavior, consult the mod_rewrite reference. (httpd.apache.org)

I now suspect it's a job for mod_rewrite: after all you can change the URL to have the www in if the user doesn't type it:
RewriteCond %{HTTP_HOST} ^mydomain.co.uk
RewriteRule (.*) [R=301,L]

So how do I Rewrite all filename.php requests on my site to filename

(no .php)?
cheers
geekpie

Finally, I found a reference for this. Ok I'm fairly new to this. I'm assuming I need apache with my hosting.

Where can I find a good reference for learning how to have all the files on my website say www.website.com/file/filename.php to just say ?

I noticed it has something to do with a htaaccess file..

can anyone explain or send me in the right direction?

Thanks

Mike

in your .htaccess

Options +Multiviews

see

"Using a 'MultiViews' search, where the server does an implicit filename pattern match and chooses from among the results. "

in your .htaccess

Options +Multiviews

see

"Using a 'MultiViews' search, where the server does an implicit filename pattern match and chooses from among the results. "

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.