.htaccess - 如何使用 Htaccess 在 php 中将页面重写为带有 id 的基本 url
问题描述
请帮我解决这个问题,我们从过去几天开始面临这个问题。
我的问题.htaccess
我已经设置了我的$base_url = http://localhost/example.com/
喜欢这个
我有一个如下所示的 URL:
http://localhost/example.com/test.php?id=10
我想像这样转换这个网址:
http://localhost/example.com/10
这是我的.Htaccess
代码
RewriteEngine On
<ifModule mod_headers.c>
Header set Connection keep-alive
</ifModule>
## EXPIRES CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access 1 year"
ExpiresByType image/jpeg "access 1 year"
ExpiresByType image/gif "access 1 year"
ExpiresByType image/png "access 1 year"
ExpiresByType text/css "access 1 month"
ExpiresByType text/html "access 1 month"
ExpiresByType application/pdf "access 1 month"
ExpiresByType text/x-javascript "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 1 month"
</IfModule>
## EXPIRES CACHING ##
#AddOutputFilterByType DEFLATE text/plain
#AddOutputFilterByType DEFLATE text/html
#AddOutputFilterByType DEFLATE text/xml
#AddOutputFilterByType DEFLATE text/css
#AddOutputFilterByType DEFLATE application/xml
#AddOutputFilterByType DEFLATE application/xhtml+xml
#AddOutputFilterByType DEFLATE application/rss+xml
#AddOutputFilterByType DEFLATE application/javascript
#AddOutputFilterByType DEFLATE application/x-javascript
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^([^\.]+)$ $1.html [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)/?$ $0.php [NC,L]
RewriteRule ^user/profile/([A-Za-z0-9-\s&/()+']+) user-profile-edit.php?name=$1 [NC,B,L]
RewriteRule ^user/profile user-profile.php [NC,B,L]
RewriteRule ^user/dashboard user-dashboard.php [NC,B,L]
RewriteRule ^user/address user-addresses.php [NC,B,L]
RewriteRule ^user/add-address user-address-add.php [NC,B,L]
RewriteRule ^user/booking user-booking.php [NC,B,L]
# Condition For Test Page
RewriteCond %{QUERY_STRING} (^|&)id=\$1($|&)
RewriteRule ^tests\.php$ /$1?&%[R=301,L]
那么如何解决这个问题呢?
解决方案
在里面example.com/.htaccess
你可以试试这些规则:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+([^/]+)/test\.php\?id=([\w-]+)\sHTTP [NC]
RewriteRule ^ /%1/%2? [R=302,L,NE]
RewriteRule ^user/profile/([-\w\s&/()+']+)/?$ user-profile-edit.php?name=$1 [NC,QSA,L]
RewriteRule ^user/profile/?$ user-profile.php [NC,L]
RewriteRule ^user/dashboard/?$ user-dashboard.php [NC,L]
RewriteRule ^user/address/?$ user-addresses.php [NC,L]
RewriteRule ^user/add-address user-address-add.php [NC,L]
RewriteRule ^user/booking/?$ user-booking.php [NC,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+?)/?$ $1.html [L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
RewriteRule ^([\w-]+)/?$ test.php?id=$1 [QSA,L]
推荐阅读
- apollo-client - 如何让所有可见的 Vue 组件重新获取它们的 Apollo 查询?
- swift - 如何在 Swift 中集成套接字 I/O?
- vba - Excel SQL 在 PowerPoint 中工作以选择和调整图片大小
- html - 输入模式不匹配,而它似乎在 regex101 中工作
- aem - 如何在给定路径下和仅在 AEM 6 中使用查询生成器激活的页面下执行全文搜索?
- python - python脚本中的命令行给出语法错误
- ios - 如何在swift 3中首次启动应用程序时导航另一个页面
- ios - 如何在ios的UITableview中聚焦最后一个单元格
- c++ - 带有朗伯着色的简单光线追踪,混淆
- javascript - javascript onclick 和 ontouch