.htaccess - htaccess - 从 URL 中删除目录名称
问题描述
我有以下站点结构:
- index.php
- css/
- js/
- img/
- content/
- product/
- product-page-1.php
- product-page-2.php
- static/
- faqs.php
- landing/
- homeware-gifts.php
- stationary/
- desks.php
所以目前我的一些网址是这样的,但我需要隐藏content
和product, static, landing
目录
产品:
mysite.com/content/product/product-page-2.php
至
mysite.com/product-page-2
静态:
mysite.com/content/static/faqs.php
至
mysite.com/faqs
着陆:
mysite.com/content/landing/homeware-gifts.php
mysite.com/content/landing/stationary/desks.php
至
mysite.com/homeware-gifts
mysite.com/stationary/desks
注意我还想隐藏 .php 扩展名,我目前正在使用这个:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
RewriteCond %{REQUEST_FILENAME}.php -f [NC]
RewriteRule ^ %{REQUEST_URI}.php [L]
哪个有效,但需要确保它也保持不变
解决方案
您可以在站点根目录 .htaccess 中使用这些规则:
RewriteEngine On
## hide .php extension
# To externally redirect /content/file.php to /file
RewriteCond %{THE_REQUEST} \s/+site1/content/(.+?)\.php[\s?] [NC]
RewriteRule ^ /site1/%1 [R=301,NE,L]
## To internally rewrite /file to /content/file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/site1/content/$1.php -f
RewriteRule ^site1/(.+?)/?$ site1/content/$1.php [L,NC]
这假设没有.htaccess
内部site1/
或其他任何地方。
推荐阅读
- javascript - 如何避免 forEach 循环在 Firebase 云函数中获取数据?
- spring - 是否可以将 Spring 的 HttpFirewall 配置为仅在某些 URL-s 上调用?
- javascript - 使用图像填充画布对象
- google-apps-script - 如何将 Tableau 仪表板放入谷歌表单?
- jquery - 如何在定义为 jQuery 对象的 javascript var 上使用 jQuery .each?
- python - 如何在导入 requests-html 模块进行网页抓取时克服回溯?
- r - 使用separate_rows() 和tidyverse 删除标点符号并合并术语
- steam - 从 Steam Api 和 IGDB 同步数据
- python - Pandas 根据其他列的值从另一个更新一个 df
- android - 从未调用过带有 RSSI_CHANGED_ACTION 的广播接收器