gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 07:25:01 25 4
gpt4 key购买 nike

我正在尝试重写此 URL:http://localhost/mysite/admin/edit-post.php?id=12
预期输出 URL:http://localhost/mysite/admin/edit/12
我的网站 是我的 根文件夹
管理员 是我的根文件夹内的子文件夹
编辑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>

最佳答案

与您展示的尝试, sample ;请尝试遵循 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]

关于.htaccess - 如何使用htaccess重写子文件夹中文件的URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68263768/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com