使用 HTACCESS 的 SEO 友好 URL
SEO Friendly URI using HTACCESS
我是 PHP 的新人。我的 URL 如下所示
http://localhost/index.php?page=20
http://localhost/user.php?id=15&localtion=delhi
http://localhost/folder/profile.php?id=15&active=today
我正在寻找的转换我的 URL 如下所示
http://localhost/page/20
http://localhost/user/id/15/localtion/delhi
http://localhost/folder/profile/id/15/active/today
我花了好几个小时尝试让它与 .htaccess 文件一起工作,但还没有成功
其中一个小的工作代码如下所示
RewriteEngine on
RewriteRule ^(.+)\.php$ / [R,L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ /.php [NC,END]
它从 URL 中删除了 .php,如下所示
http://localhost/index?page=15
我已经在 Whosebug 中检查了很多问题和答案,如下所示
但无法实现我的目标。让我知道这里是否有任何专家可以帮助我做同样的事情,而且我的 php 代码仍然可以正常工作,没有任何问题。
非常感谢!
使用您显示的示例,请尝试遵循 htaccess 规则。请确保在测试您的 URL 之前清除您的浏览器缓存。
RewriteEngine ON
##Rule for external rewrite.
##Rule to handle from http://localhost/index.php?page=20 to http://localhost/page/20
RewriteCond %{THE_REQUEST} \s/index\.php\?([\w-]+)=(\d+)\s [NC]
RewriteRule ^ /%1/%2? [R=301,L]
##Rule for external rewrite.
##Rule to handle from http://localhost/user.php?id=15&localtion=delhi TO http://localhost/user/id/15/localtion/delhi
RewriteCond %{THE_REQUEST} \s/(user)\.php\?([\w-]+)=(\d+)&([\w-]+)=([\w-]+)\s [NC]
RewriteRule ^ /%1/%2/%3/%4/%5? [R=301,L]
##Rule for external rewrite.
##Rule to handle from http://localhost/folder/profile.php?id=15&active=today To http://localhost/folder/profile/id/15/active/today
RewriteCond %{THE_REQUEST} \s/(folder)/(profile)\.php\?([\w-]+)=(\d+)&([\w-]+)=([\w-]+)\s [NC]
RewriteRule ^ /%1/%2/%3/%4/%5/%6? [R=301,L]
##internal rewrite rule to handle files internally.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)/?$ /.php?=&= [QSA,L]
##internal rewrite rule to handle files internally.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)/?$ .php?=&= [QSA,L]
##internal rewrite rule to handle files internally.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/?$ index.php?= [QSA,L]
我是 PHP 的新人。我的 URL 如下所示
http://localhost/index.php?page=20
http://localhost/user.php?id=15&localtion=delhi
http://localhost/folder/profile.php?id=15&active=today
我正在寻找的转换我的 URL 如下所示
http://localhost/page/20
http://localhost/user/id/15/localtion/delhi
http://localhost/folder/profile/id/15/active/today
我花了好几个小时尝试让它与 .htaccess 文件一起工作,但还没有成功
其中一个小的工作代码如下所示
RewriteEngine on
RewriteRule ^(.+)\.php$ / [R,L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ /.php [NC,END]
它从 URL 中删除了 .php,如下所示
http://localhost/index?page=15
我已经在 Whosebug 中检查了很多问题和答案,如下所示
但无法实现我的目标。让我知道这里是否有任何专家可以帮助我做同样的事情,而且我的 php 代码仍然可以正常工作,没有任何问题。
非常感谢!
使用您显示的示例,请尝试遵循 htaccess 规则。请确保在测试您的 URL 之前清除您的浏览器缓存。
RewriteEngine ON
##Rule for external rewrite.
##Rule to handle from http://localhost/index.php?page=20 to http://localhost/page/20
RewriteCond %{THE_REQUEST} \s/index\.php\?([\w-]+)=(\d+)\s [NC]
RewriteRule ^ /%1/%2? [R=301,L]
##Rule for external rewrite.
##Rule to handle from http://localhost/user.php?id=15&localtion=delhi TO http://localhost/user/id/15/localtion/delhi
RewriteCond %{THE_REQUEST} \s/(user)\.php\?([\w-]+)=(\d+)&([\w-]+)=([\w-]+)\s [NC]
RewriteRule ^ /%1/%2/%3/%4/%5? [R=301,L]
##Rule for external rewrite.
##Rule to handle from http://localhost/folder/profile.php?id=15&active=today To http://localhost/folder/profile/id/15/active/today
RewriteCond %{THE_REQUEST} \s/(folder)/(profile)\.php\?([\w-]+)=(\d+)&([\w-]+)=([\w-]+)\s [NC]
RewriteRule ^ /%1/%2/%3/%4/%5/%6? [R=301,L]
##internal rewrite rule to handle files internally.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)/?$ /.php?=&= [QSA,L]
##internal rewrite rule to handle files internally.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)/?$ .php?=&= [QSA,L]
##internal rewrite rule to handle files internally.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/?$ index.php?= [QSA,L]