How can mod_rewrite be used to optimize websites for search engines in PHP?
To optimize websites for search engines using mod_rewrite in PHP, you can create SEO-friendly URLs that are easier for search engines to crawl and index. This can improve your website's visibility and ranking in search engine results pages.
RewriteEngine On
# Redirect non-www to www
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
# Rewrite URLs to remove file extensions
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Keywords
Related Questions
- What are some best practices for handling and extracting specific data, such as movie genres, from external websites using PHP?
- In what scenarios should the caret (^) and dollar sign ($) be used in regular expressions in PHP to ensure accurate matching?
- What are best practices for integrating PHP forms with MySQL databases, especially when adding new fields like last name?