首页 > 解决方案 > 如何使用 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]

那么如何解决这个问题呢?

标签: .htaccessmod-rewriteurl-rewriting

解决方案


在里面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]

推荐阅读