Reputation: 55
I am new and beginner for URL Rewrite... I need to remove .php extension for these pages for menu links.
<ul class="nav">
<li><a href="index.php">Home</a></li>
<li><a href="about.php">About</a></li>
<li><a href="services.php">Services</a></li>
<li><a href="rates.php">Rates</a></li>
<li><a href="contact.php#main">Contact Us</a></li>
</ul>
I am using the following code for .htaccess file
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
# Remove .php-extension from url
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^\.]+)/$ $1.php
# End of Apache Rewrite Rules
</IfModule>
For example:
localhost/websitename/index.php
to
localhost/websitename/index
localhost/websitename/about.php
to
localhost/websitename/about
localhost/websitename/services.php
to
localhost/websitename/services
localhost/websitename/rates.php
to
localhost/websitename/rates
localhost/websitename/contact.php#main
to
localhost/websitename/contact-main
But the following Rewrite Rule is showing the result like this
localhost/websitename/services.php
to
localhost/services
........... ERROR: Page not Found
.
.
.
.
.
so on and on...
What I am doing wrong? How can I achieve this simple URL Rewrite?
Upvotes: 1
Views: 1277
Reputation: 785256
For hiding .php
extension replace your code with this:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php[\s?] [NC]
RewriteRule ^ %1 [R=301,L]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/gta/$1.php -f
RewriteRule ^(.+?)/?$ /gta/$1.php [L]
Upvotes: 1
Reputation: 15374
Looks like you're just missing part of the path to your file on disk:
RewriteRule ^([^\.]+)$ websitename/$1.php
Upvotes: 0