简体   繁体   English

在PHP中使用.htaccess的虚荣网址

[英]vanity url with .htaccess in php

I'm trying to create a vanity url for where users profile could easily be viewed by others. 我正在尝试创建一个虚荣网址,以便其他人可以轻松查看用户个人资料。

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} -f [OR]

RewriteCond %{REQUEST_FILENAME} -d

RewriteRule .* - [L]

RewriteRule ^(.*)$ http://localhost/test/public/profile.php?username=$1 [NC]

This is the code but each time i use this code in my .htaccess file, i get 500 Internal Server Error and the sub-folder public disappears from my directory. 这是代码,但是每次我在.htaccess文件中使用此代码时,都会收到500 Internal Server Error,并且子文件夹public从我的目录中消失。 What am i getting wrong? 我怎么了? Do i need any to do any configuration first? 我需要先进行任何配置吗?

It's better to use global rewrite logic: 最好使用全局重写逻辑:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]

and in Your index.php catch $_SERVER['REQUEST_URI'] and if user exists so respond if not then pass processing to next procedure. 并在您的index.php中捕获$ _SERVER ['REQUEST_URI'] ,如果用户存在,则响应,否则将处理传递到下一个过程。

But if You insist on realization as in question so try this: 但是,如果您坚持要实现,请尝试以下操作:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ profile.php?username=$1 [L,QSA]

Check this: 检查一下:

    Options +FollowSymLinks
    <IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^profile/([a-zA-Z0-9_\-/]+)$ test/public/profile.php?username=$1 [L,NC]
    </IfModule>

However note: 但是请注意:

  1. You must have the rewrite engine on to make it work (google a bit for the installation procedure the OS/system you are working) 您必须启用重写引擎才能使其正常工作(对于正在使用的OS /系统的安装过程,请花点时间搜索Google)
  2. Put proper RewriteBase directive if you use it from subdirectory. 如果从子目录中使用它,则放置正确的RewriteBase指令。

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

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