微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用htaccess重写子文件夹中文件的URL

如何解决如何使用htaccess重写子文件夹中文件的URL

我正在尝试重写此 URL:http://localhost/mysite/admin/edit-post.PHP?id=12

预期输出网址:http://localhost/mysite/admin/edit/12

mysite 是我的文件

admin 是我的根文件夹内的子文件

edit-post.PHP 文件在我的子文件夹内

我的 htaccess 代码

# 404 page
ErrorDocument 404 http://localhost/mysite/404.PHP

RewriteEngine On
RewriteBase /mysite/

# Extension Removal
RewriteCond %{REQUEST_FILENAME}\.PHP -f
RewriteRule ^(.*)$ $1.PHP

# URL Rewrite
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# This Rule is for the file which i have it in my root folder,It's working
RewriteRule ^posts/(.+)$ post.PHP?id=$1 [L]

# And I'm tring the same but it's not working. 
RewriteRule ^edit/(.+)$ edit-post.PHP?id=$1 [L]

我的PHP代码

<a href="edit/<?PHP echo $post_detail['id']; ?>">Edit Post</a>

解决方法

根据您显示的尝试,示例;请尝试遵循 htaccess 规则文件。

请确保在测试您的 URL 之前清除浏览器缓存。

# 404 page
ErrorDocument 404 http://localhost/mysite/404.php

RewriteEngine On
RewriteBase /mysite/

# Extension Removal
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

# URL Rewrite
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# This Rule is for the file which i have it in my root folder,It's working
RewriteRule ^posts/(.+)$ post.php?id=$1 [L]

###Newly added rule here,where considering that your edit-post.php is present inside admin folder.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^admin/edit/(.+)/?$ admin/edit-post.php?id=$1 [L]

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。