如何使用 .htaccess 从 url 中隐藏文件名

编程入门 行业动态 更新时间:2024-10-24 18:25:26
本文介绍了如何使用 .htaccess 从 url 中隐藏文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想知道如何使用 .htaccess 从 url 中完全隐藏文件名.

目前是这样的:mysite/文件夹名/文件名

我希望它是这样的:mysite/文件夹名

目前我使用 .htacces 文件仅从 url 中删除文件扩展名,我想将其升级为仅显示文件夹名称.我们还没有领导任何关于 .htacces 的事情,所以我很感激非常直接的代码和提示,谢谢!:)

RewriteOptions 继承重写引擎开启RewriteCond %{REQUEST_FILENAME} !-dRewriteCond %{REQUEST_FILENAME}.php -f重写规则 ^(.*)$ $1.phpRewriteCond %{REQUEST_FILENAME} !-dRewriteCond %{REQUEST_FILENAME}.html -f重写规则 ^(.*)$ $1.htmlRewriteCond %{REQUEST_FILENAME} !-dRewriteCond %{REQUEST_FILENAME}.htm -f重写规则 ^(.*)$ $1.htmRewriteCond %{HTTP_HOST} ^.*$RewriteRule ^/?$ "tjmediaproductions/quiz/" [R=301,L]

解决方案

RewriteOptions inheritRewriteEngine On # 启用 url 重写RewriteCond %{REQUEST_FILENAME} !-d # 如果请求的 uri 不是目录 (!-d)RewriteCond %{REQUEST_FILENAME}.php -f # 如果有一个名为 URI+'.php' 的文件 (-f)RewriteRule ^(.*)$ $1.php # 那么如果uri中有任何东西则将其重写为uri+'.php'

其他都一样还有那部分我不知道.

<br/>

还有一点和问题如你所说目前它是这样的:mysite/foldername/filename我希望它是这样的:mysite/foldername告诉我它背后有什么样的规则?你想要这个吗

mysite/某事

插入

mysite/something/something.php

或使用

mysite/某事

插入

mysite/something/anotherthing.php

如果你想使用第一种方式.你可以使用这可能是有效的

RewriteCond %{REQUEST_FILENAME} -d # 如果请求的 uri 是目录 (-d)RewriteCond %{REQUEST_FILENAME}.php !-f # 如果没有文件 (!-f)RewriteRule ^([A-Za-z0-9-]+)/?$ $1/$1.php [NC,L] # 文件夹名/文件夹名.php

或者这个

RewriteCond %{REQUEST_FILENAME} !-d # 如果请求的 uri 不是目录 (!-d)RewriteRule ^([A-Za-z0-9-]+)/?$ $1/$1.php [NC,L] # 文件夹名/文件夹名.php

I was wondering how I could hide filename completetely from url using .htaccess.

Currently it is like: mysite/foldername/filename

And I would like it to be like: mysite/foldername

At the moment I use .htacces file for removing only file extensios from url and I would like to upgrade it to only show folder name. We have not leader anything about .htacces yet so I would appreciate pretty direct codes and hints, thank you! :)

RewriteOptions inherit RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}.php -f RewriteRule ^(.*)$ $1.php RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}.html -f RewriteRule ^(.*)$ $1.html RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}.htm -f RewriteRule ^(.*)$ $1.htm RewriteCond %{HTTP_HOST} ^.*$ RewriteRule ^/?$ "tjmediaproductions/quiz/" [R=301,L]

解决方案

RewriteOptions inherit RewriteEngine On # enables url rewriting RewriteCond %{REQUEST_FILENAME} !-d # if requested uri is not directory (!-d) RewriteCond %{REQUEST_FILENAME}.php -f # and if there is a file named URI+'.php' (-f) RewriteRule ^(.*)$ $1.php # then if there is any thing in uri then rewrite it as uri+'.php'

others are the same and about the las part idont know.

<br /> <br />

and there is another point and question as you said Currently it is like: mysite/foldername/filename And I would like it to be like: mysite/foldername tell me what kind of rule is behind it? do you want to have this

mysite/something

insted of

mysite/something/something.php

or use

mysite/something

insted of

mysite/something/anotherthing.php

if you want to use first way. you can use this may be works

RewriteCond %{REQUEST_FILENAME} -d # if requested uri is directory (-d) RewriteCond %{REQUEST_FILENAME}.php !-f # and if there is not a file (!-f) RewriteRule ^([A-Za-z0-9-]+)/?$ $1/$1.php [NC,L] # foldername/foldername.php

or maybe this

RewriteCond %{REQUEST_FILENAME} !-d # if requested uri is not directory (!-d) RewriteRule ^([A-Za-z0-9-]+)/?$ $1/$1.php [NC,L] # foldername/foldername.php

更多推荐

如何使用 .htaccess 从 url 中隐藏文件名

本文发布于:2023-10-31 18:41:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1546944.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件名   如何使用   htaccess   url

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!