.htaccess 和 mod_rewrite 的链接口是心非问题

发布于 2024-09-25 09:35:13 字数 868 浏览 0 评论 0原文

抱歉,我无法想出更好的标题。

这是我的 htaccess

RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L,QSA]

这是我的问题:

localhost/validpage 给了我 localhost/validpage.php 的内容。 localhost/validpage/blah 也给了我相同的(validpage.php)页面, localhost/validpage/blah/blah/... 因此,我面临的问题是链接口是心非(用我的话来说!)。

如何允许仅从 localhost/validpage 访问 localhost/validpage.php,而不是其他内容,甚至不从 localhost/validpage.php 访问。

我也开始了关于 *Server****Fault*** 的问题,但是但没有取得多大成功。 我得到的答案是仅用 htaccess 无法完成。

我所说的有效页面是指服务器上的任何有效页面。由于我正在使用 mod_rewrite 改造现有网站以获得更清晰的网址,因此我正在寻找一个相对简单的解决方案,最好仅使用 .htaccess。但是,任何解决方案都是受欢迎的。

Sorry I couldn't come up with a better title.

Here is my htaccess

RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L,QSA]

And this is my problem:

localhost/validpage gives me the contents of localhost/validpage.php.
localhost/validpage/blah also gives me the same (validpage.php) page and so does localhost/validpage/blah/blah/...
Therefore the problem I am facing is link duplicity(in my words!).

How do I allow localhost/validpage.php to be accessed from localhost/validpage only and nothing else, not even localhost/validpage.php.

I have started a question on *Server****Fault*** too but with not much success.
The answer I have got is it cannot be done with htaccess alone.

By validpage I mean any valid page on the server. Since I am retrofitting an existing site with mod_rewrite for cleaner urls, I am looking for a relatively easy solution preferably with .htaccess only. However, any solutions are welcome.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

隐诗 2024-10-02 09:35:13

你的图像等的来源属性是什么???
绝对还是相对?

指向不同的文件应用重写规则时。

what is the source attribute of your images, etc ???
absolute or relative?

<img src="/images/my.jpg" /> and <img src="images/my.jpg" /> point to different files when applying your rewrite rules.

烟沫凡尘 2024-10-02 09:35:13

您可以尝试在 .htaccess 中使用它,

RewriteEngine on
RewriteBase /

# if request has a php extension remove and redirect
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_URI} ^((.*)\.php)$
RewriteRule ^(.*).php$ $1 [L,R=301]

# if request uri has no extension link to php file
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L]

我应该将您的 php 脚本重写为友好的 url,并使用 .php 扩展名重定向请求。

You could try using this in your .htaccess

RewriteEngine on
RewriteBase /

# if request has a php extension remove and redirect
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_URI} ^((.*)\.php)$
RewriteRule ^(.*).php$ $1 [L,R=301]

# if request uri has no extension link to php file
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L]

I should rewrite your php scripts to friendly urls, and redirect requests using the .php extension.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文