简体   繁体   English

如何自动制作漂亮的网址

[英]How to automatically make pretty urls

I have a site that I'm working on, but I'm annoyed that I have to work with ugly URLS. 我有一个正在处理的网站,但是我不得不使用丑陋的URL感到很烦。 So, I have a URL of http://example.com/user.php?id=54 and another of http://example.com/foobar.php?name=Test . 因此,我有一个http://example.com/user.php?id=54的URL和另一个http://example.com/foobar.php?name=Test的URL。

How could I convert both of them to pretty URLS without adding it to .htaccess for every URL I want to make pretty? 我如何将它们都转换为漂亮的URL,而又不将它们添加到要创建漂亮的每个URL的.htaccess中?

example.com/user.php?id=54 => example.com/user/54 example.com/user.php?id=54 => example.com/user/54

example.com/foobar.php?name=Test => example.com/foobar/Test example.com/foobar.php?name=Test => example.com/foobar/Test

I have this in my .htaccess file: 我的.htaccess文件中有此文件:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
RewriteRule ^([^/]+)/?$ $1.php [L]
RewriteRule ^$1/$3/? /$1.php?$2=$3 [NC]

Thanks, Lucy 谢谢露西

My full .htaccess file: 我完整的.htaccess文件:

# include classes on every page
php_value auto_prepend_file Resources/Classes.php

# add custom Directory Indexes
DirectoryIndex index.php Default.php Down.php

# begin routing to pretty URLs
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^/(?!Resources)([0-9a-zA-Z-]+)/([0-9]+) /$1.php?id=$2 [NC]
RewriteRule ^/(?!Resources)([0-9a-zA-Z-]+)/([a-zA-Z-]+) /$1.php?name=$2 [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]

Try this 尝试这个

Options +FollowSymLinks
RewriteEngine On
RewriteRule ^user/([0-9]+) /user.php?id=$1 [QSA,L]
RewriteRule ^foobar/([0-9a-zA-Z-]+) /foobar.php?name=$1 [QSA,L]

if you want global rule you can make 如果您想要全局规则,则可以制定

Options +FollowSymLinks
RewriteEngine On
RewriteRule ^([0-9a-zA-Z-]+)/([0-9a-zA-Z-]+) /$1.php?parameter=$2 [NC]

or more specifically 或更具体地

Options +FollowSymLinks
RewriteEngine On
RewriteRule ^([0-9a-zA-Z-]+)/([0-9]+) /$1.php?id=$2 [NC]
RewriteRule ^([0-9a-zA-Z-]+)/([a-zA-Z-]+) /$1.php?name=$2 [NC]

when argument will be a string it will pas name parameter and when argument will be integer there will be id parameter passed. 当参数是一个字符串时,它将传递name参数;当参数是整数时,将传递id参数。

I may delete this answer in the future as it might be specific to my setup. 以后我可能会删除此答案,因为它可能特定于我的设置。


I recently discovered, using Apache, that anything after the URL was populating the PATH_INFO environment variable. 我最近发现使用Apache,在URL填充PATH_INFO环境变量之后的所有操作。 This means that given your example, example.com/user/54 , if user was a script the server could process, anything after it would be populated into PATH_INFO; 这意味着,在您的示例example.com/user/54 ,如果user是服务器可以处理的脚本,则该脚本中的所有内容都将填充到PATH_INFO中; in this case it would look like /54 . 在这种情况下,它看起来像/54 This is a great find because with proper structure, you could make your own router similar to Rails. 这是一个很好的发现,因为如果结构正确,您可以使自己的路由器类似于Rails。

I would create some landing page (eg, index ) which is going to be your application router: example.com/index/<model>/<id>/ . 我将创建一些登陆页面(例如index ),它将作为您的应用程序路由器: example.com/index/<model>/<id>/ Inside index would be your routing code. 内部索引将是您的路由代码。 I'll use Perl to demonstrate, since it's better than PHP :) Note that index could be called anything that Apache can process (eg, router.php , index.pl , application.rb ); 我将使用Perl进行演示,因为它比PHP更好:)请注意, 索引可以称为Apache可以处理的任何东西(例如, router.phpindex.plapplication.rb ); though, removing the extension adds to the beauty of the URL. 不过,删除扩展名可以增加URL的美感。

index: 指数:

#!/usr/bin/perl
use 5.012;

# Retrieve what you're looking for; obviously not production-ready
my ($model,$id) = $ENV{PATH_INFO} =~ m{^/([^/]+?)/([^/]+)};

# route the request
given($model){
   when('user'){ callUser($id); }     # callUser defined elsewhere, perhaps another script
   when('foobar'){ callFoobar($id); } # callFoobar defined elsewher, perhaps another script
   default { makePageDefault(); }
}

The script above is not production ready because it doesn't perform any sanitation and doesn't handle all the use cases you will need. 上面的脚本尚未准备好投入生产,因为它不执行任何卫生措施,也无法处理您将需要的所有用例。 My guess is you want something similar to Rails (eg, example.com/movie/1/edit ). 我的猜测是您想要类似于Rails的东西(例如example.com/movie/1/edit )。 While Apache is designed to handle the routing for you, there is some convenience in being able to manage this close to where your application code lives. 尽管Apache是​​为您处理路由而设计的,但是能够在您的应用程序代码所在的位置附近进行管理有一些便利。

I have not implemented this method, so I'm curious to hear if this is something used and if there's any reason not to trust it. 我尚未实现此方法,因此我很想知道是否使用了这种方法以及是否有任何理由不信任它。

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

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