使用.htaccess重写URL以进行登录和配置文件系统

时间:2021-11-21 11:18:37

I have a website where the url is localhost/project/profile.php?user=usernameand I am trying to get the url to look like this: localhost/project/username

我有一个网站,网址是localhost / project / profile.php?user = username我试图让网址看起来像这样:localhost / project / username

The most I am able to do is get rid of the .php by using the following code:

我能做的最多就是使用以下代码摆脱.php:

Options +FollowSymLinks -MultiViews

RewriteEngine On
RewriteBase /

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]

but that is not what I need right now. Here is the code that is meant to be changing the url - I got it off of another thread.

但这不是我现在所需要的。这是用于更改url的代码 - 我将其从另一个线程中删除。

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

but it obviously doesn't work.

但它显然不起作用。

I need to be able to go to the url: localhost/project/username and it registers as the original URL localhost/project/profile.php?user=username

我需要能够转到url:localhost / project / username并将其注册为原始URL localhost / project / profile.php?user = username

** THE ANSWER **

** 答案 **

Thanks to Howlin

感谢Howlin

RewriteEngine On

RewriteCond %{THE_REQUEST} ^(GET|POST)\ /project/profile\.php\?user=(.*)\ HTTP
RewriteRule ^ /project/%2\? [R=301,L]

RewriteCond %{QUERY_STRING} !user=
RewriteRule ^(.*)$ /project/profile.php?user=$1 [L]

2 个解决方案

#1


0  

This should work:

这应该工作:

RewriteEngine On
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /project/profile\.php\?user=(.*)\ HTTP
RewriteRule ^ /project/%2\? [R=301,L]

RewriteCond %{QUERY_STRING} !user=
RewriteRule ^project/(.*)$ /project/profile.php?user=$1 [L]

The above will change project/profile.php?user=username to project/username and it will load the information from the project/profile.php?user=username page

以上将把project / profile.php?user = username更改为project / username,它将从project / profile.php?user = username页面加载信息

EDIT:

RewriteEngine On

RewriteCond %{THE_REQUEST} ^(GET|POST)\ /project/profile\.php\?user=(.*)\ HTTP
RewriteRule ^ /project/%2\? [R=301,L]

RewriteCond %{QUERY_STRING} !user=
RewriteCond %{REQUEST_FILENAME} !-d  
RewriteCond %{REQUEST_FILENAME} !-f  
RewriteRule ^(.*)$ /project/profile.php?user=$1 [L]

#2


0  

Try this:

Options FollowSymLinks

RewriteEngine On
RewriteBase /project/

RewriteRule ^([^/]+)/?$ profile\.php?user=$1 [L,NC,QSA]

#1


0  

This should work:

这应该工作:

RewriteEngine On
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /project/profile\.php\?user=(.*)\ HTTP
RewriteRule ^ /project/%2\? [R=301,L]

RewriteCond %{QUERY_STRING} !user=
RewriteRule ^project/(.*)$ /project/profile.php?user=$1 [L]

The above will change project/profile.php?user=username to project/username and it will load the information from the project/profile.php?user=username page

以上将把project / profile.php?user = username更改为project / username,它将从project / profile.php?user = username页面加载信息

EDIT:

RewriteEngine On

RewriteCond %{THE_REQUEST} ^(GET|POST)\ /project/profile\.php\?user=(.*)\ HTTP
RewriteRule ^ /project/%2\? [R=301,L]

RewriteCond %{QUERY_STRING} !user=
RewriteCond %{REQUEST_FILENAME} !-d  
RewriteCond %{REQUEST_FILENAME} !-f  
RewriteRule ^(.*)$ /project/profile.php?user=$1 [L]

#2


0  

Try this:

Options FollowSymLinks

RewriteEngine On
RewriteBase /project/

RewriteRule ^([^/]+)/?$ profile\.php?user=$1 [L,NC,QSA]