繁体   English   中英

带有htaccess的PHP漂亮URL,无需更改URL

[英]PHP Pretty URLs with htaccess without changing the URL

我搜索了许多类似的问题,但找不到答案。 我有一个拥有用户的php网站,我想创建用户个人资料页面。

现在,它的工作方式如下:

https://example.com/profile?userid=user_id_here

但我希望它像这样工作:

https://example.com/profile/user_id_here

用户单击其个人资料网址时,即:

https://example.com/profile/user_id_here

我希望页面以静默方式将此网址称为:

https://example.com/profile?userid=user_id_here

因此,用户将始终看到友好的URL,而没有问号和userid =部分。

我不希望这个页面(重定向https://example.com/profile?userid=user_id_here )此页面( https://example.com/profile/user_id_here ),因为用户不会看到这一个( HTTPS: //example.com/profile?userid=user_id_here )。

我只想让profile.php静静地调用特殊的url。

我当前的重写规则在下面,并且我希望新规则在下面添加规则:

RewriteEngine On

# match any URL with www and rewrite it to https without the www
RewriteCond %{HTTP_HOST} ^(www\.)(.*) [NC]
RewriteRule (.*) https://%2%{REQUEST_URI} [L,R=301]

# match urls that are non https (without the www)
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^(www\.)(.*) [NC]
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

#remove .php
RewriteRule ^(.+)\.php$ /$1 [R,L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ /$1.php [NC,END]

我为此花费了超过2天的时间,但找不到解决方案。 感谢您对规则执行情况的任何帮助。 先感谢您。

试试这个,我还没有测试,但是应该可以

RewriteRule ^profile/([^/]*)$ /profile?userid=$1 [L]

尝试这个,

DirectoryIndex index.php
Options +FollowSymLinks 
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^(.*)$ index.php/$1 [L,QSA]

您可以使用:

RewriteEngine On

# match any URL with www and rewrite it to https without the www
RewriteCond %{HTTP_HOST} ^(www\.)(.*) [NC]
RewriteRule (.*) https://%2%{REQUEST_URI} [L,R=301]

# match urls that are non https (without the www)
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^(www\.)(.*) [NC]
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

#remove .php
RewriteRule ^(.+)\.php$ /$1 [R,L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ /$1.php [NC,END]

#rewrite /profile/userID to /profile?userid=userID
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^profile/(.+)$ /profile?userid=$1 [L,NC]

RewriteConds确保您不将现有目录和文件重写为/profile?userid

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM