URL在使用htaccess删除扩展后不能使用斜杠

时间:2021-10-13 10:39:05

I have been working on localhost, and my htaccess file is

我一直在使用localhost,我的htaccess文件是

Options +FollowSymLinks 
Options -Indexes
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) $1.php [L]

after adding the htacces code,the url

添加了htacces代码后,url

localhost/movies/news.php

works

作品

localhost/movies/news

also works but

也可以,但

localhost/movies/news/

doesn't work. It shows "Internal Server Error".How to make it work with slash and without slash.

是行不通的。它显示“内部服务器错误”。如何使它与斜线和无斜线一起工作。

2 个解决方案

#1


2  

You an try this code:

你可以试试这个代码:

Options +FollowSymLinks -MultiViews
RewriteEngine On

# Internally forwards movies/news/ to movies/news.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

#2


1  

The problem is when you add the slash you have news/.php and this is not working.

问题是当你添加斜线时,你有新闻/。php不能运行。

A better solution is to rewrite to a GET variable something like this:

更好的解决方案是重写为GET变量,如下所示:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) index.php?url=$1 [L]

Then you can filter the GET variable in your script and include the file or content you need.

然后可以在脚本中过滤GET变量,并包含所需的文件或内容。

#1


2  

You an try this code:

你可以试试这个代码:

Options +FollowSymLinks -MultiViews
RewriteEngine On

# Internally forwards movies/news/ to movies/news.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

#2


1  

The problem is when you add the slash you have news/.php and this is not working.

问题是当你添加斜线时,你有新闻/。php不能运行。

A better solution is to rewrite to a GET variable something like this:

更好的解决方案是重写为GET变量,如下所示:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) index.php?url=$1 [L]

Then you can filter the GET variable in your script and include the file or content you need.

然后可以在脚本中过滤GET变量,并包含所需的文件或内容。