URL Rewrite -.htaccess

核能气质少年 提交于 2019-12-08 12:23:15

问题


Simple URL Rewriting for removing .php extensions from the links I am using the following code on the .htaccess file

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}/$1.php -f
RewriteRule ^(.+?)/?$ /$1.php [L]

After uploading the site on the hosting server the above mod_rewrite code lead the pages to the following links as I needed...

domain.com/abc/index
domain.com/abc/about
domain.com/abc/services
domain.com/abc/rates
domain.com/abc/contact#main

but showing (404 Page Not Found ERROR) for these pages i.e Not showing the exact pages like index.php, about.php, services.php, rates.php and contact.php#main.... etc

Can anybody please let me know what I am missing? As the Menu links on the web pages like

<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>

Should I have to do something with these menu links too?


回答1:


Move your .php files from:

public_html/*.php

To:

public_html/gta/*.php

What happens is that you're accessing:

http://domain.com/gta/about

And it tries to find the .php file at:

http://domain.com/gta/about.php

But from what you told me the files are on the previous folder so it's only right it will give you a 404 error.

Given that all files are on the folder gta you .htaccess can be what you exactly have, but needs to be on the public_html folder and there should be NO .htaccess inside the gta folder:

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}/$1.php -f
RewriteRule ^(.+?)/?$ /$1.php [L]


来源:https://stackoverflow.com/questions/18929925/url-rewrite-htaccess

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!