如何使用 htaccess 重写子文件夹中文件的 URL
How to rewrite the URL of file which is in the subfolder using htaccess
我正在尝试重写这个 URL : http://localhost/mysite/admin/edit-post.php?id=12
预期输出 URL:http:///localhost/mysite/admin/edit/12
mysite 是我的 rootfolder
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 ^(.*)$ .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= [L]
# And I'm tring the same but it's not working.
RewriteRule ^edit/(.+)$ edit-post.php?id= [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 ^(.*)$ .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= [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= [L]
我正在尝试重写这个 URL : http://localhost/mysite/admin/edit-post.php?id=12
预期输出 URL:http:///localhost/mysite/admin/edit/12
mysite 是我的 rootfolder
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 ^(.*)$ .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= [L]
# And I'm tring the same but it's not working.
RewriteRule ^edit/(.+)$ edit-post.php?id= [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 ^(.*)$ .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= [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= [L]