繁体   English   中英

如何为 htaccess 中的配置文件用户创建干净的 url,而我已经在使用工作正常的文章

[英]How to create clean url for profile users in htaccess while i already using for article which is working fine

这是互联网上一个非常常见的问题,但不幸的是,我还没有找到任何解决方案。 我的问题是,我已经在使用以下代码,这对于文章目的(site.com/my-first-article)来说效果很好。

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

#for pretty url
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ blog.php?qTitle=$1 [NC,L]

但是现在我需要(site.com/profile/user-name),当我复制和编辑上面的用户配置文件代码时它不起作用。

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

#for pretty url
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ profile.php?username=$1 [NC,L]

我希望我很好地定义了这个问题,并且有些人可以理解我的问题。 谢谢你的时间。

我的 output

在此处输入图像描述

例如,要将地址site.com/profile.php?username=J0R1AN更改为site.com/profile/J0R1AN ,您需要使用以下代码:

RewriteEngine On
# If user directly accesses /profile/[username]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?profile/(.*?)/?$ /profile.php?username=$1 [L]
# If user accesses /profile.php?username=[username]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /profile\.php\?username=([^\&\ ]+)
RewriteRule ^/?profile\.php$ /profile/%1? [L,R=301]

您甚至可以在 profile.php 文件中继续使用$_GET

资料来源: https://www.taniarascia.com/rewrite-query-string-to-path-with-htaccess/

暂无
暂无

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

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