在迷你虚拟社区中,我有一个profile_view功能,以便我可以查看任何注册用户的个人资料.配置文件视图功能具有配置文件所属的用户的id作为参数,因此当我想访问用户2的配置文件时,我称之为:http://127.0.0.1: 8000 /账户/ profile_view/2 /
我的问题是我想在网址中使用用户名,而不是ID.我尝试按如下方式修改我的代码,但它仍然不起作用.这是我的代码:
视图:
def profile_view(request, user):
u = User.objects.get(pk=user)
up = UserProfile.objects.get(created_by = u)
cv = UserProfile.objects.filter(created_by = User.objects.get(pk=user))
blog = New.objects.filter(created_by = u)
replies = Reply.objects.filter(reply_to = blog)
vote = Vote.objects.filter(voted=blog)
following = Relations.objects.filter(initiated_by = u)
follower = Relations.objects.filter(follow = u)
return render_to_response('profile/publicProfile.html', {
'vote': vote,
'u':u,
'up':up,
'cv': cv,
'ing': following.order_by('-date_initiated'),
'er': follower.order_by('-date_follow'),
'list':blog.order_by('-date'),
'replies':replies
},
context_instance=RequestContext(request))
Run Code Online (Sandbox Code Playgroud)
和我的网址:
urlpatterns = patterns('',
url(r'^profile_view/(?P<user>\d+)/$',
profile_view,
name='profile_view'),
Run Code Online (Sandbox Code Playgroud)
提前致谢!
我有一个类似于以下内容的网址:
https://example.com/event/FI42382
我创建了一个名为的文件夹event,我想从中使用 PHP 来处理该FI42382部分。
但是,最后一部分被视为其自己的目录。我需要做什么才能使它不是一个目录,而是可以将最后一个路径作为变量进行操作event/index.php?
抱歉,如果我的标题措辞不好。我不知道如何解释这一点。
例子:
这=> URL请求./any_folder/main.d25a1b054a0b0fbeb3def5a0ff50d01e.min.js
对于这个 => URL 请求 ./any_folder/main.min.js
我的.htaccess:
RewriteEngine On
# Remove md5 from asset files
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+).([a-fA-F0-9]{32}).(.+)?(js|css)$ $1.$3 [L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Send Requests To Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
Run Code Online (Sandbox Code Playgroud)
上面的配置不起作用。
我的目标是使用 .htaccess重定向http://ipaddress/directory/file.php?id=47到http://ipaddress/directory/47。的ID参数会有所不同,可以象1,2,3,10,编号中的任何串20,300等我当前htaccess的文件是如下:
RewriteEngine on
RewriteCond %{QUERY_STRING} id=([0-9]+) [NC]
RewriteRule (.*) /directory/%1? [R=301,L]
Run Code Online (Sandbox Code Playgroud)
重写确实将 url 更改为我想要的!但是我在重定向后收到错误消息,指出未找到 - 在此服务器上找不到请求的 URL。我不确定为什么重定向有效,但没有加载具有id参数和实际 PHP 文件的上一页。
谢谢!
拥有友好的URL通常是一件好事.但是,有时候这似乎是一个坏主意.你的经验法则是什么?
例如,考虑我想要显示注册成功页面的情况.我希望所有的底层逻辑都是一样的.但是,根据他们的注册方式,我可能希望为在某种角色下注册的人显示不同的消息.
以下是一些"hackable"(如链接中所述)URL 的袖口示例:
所有这些看起来都很糟糕,因为我不希望URL被发现.另一方面,我讨厌做一些更复杂的事情,只是为了略微修改成功消息.
你会怎么处理这个?
如何在ASP.NET MVC中创建自定义路由处理程序?
我想使用java的URLEncoder类来编码我的url字符串.但是,默认情况下,它会将空格转换为"+"(加号).我希望它使用' - '代替(类似于此网站如何编码其网址).
我想知道最好的方法是什么?我应该只使用其内置功能,然后进行字符串替换(从'+'到' - ')?谢谢
我希望在我的网站上有"漂亮"和SEO导向的URL.
我为这个网站建立了自己的小框架,现在几乎所有东西都已完成.
我仍然困惑的一件事是我将使用的漂亮/ SEO URL系统.我知道有很多方法可以实现这一点,我希望在这个方面平衡最佳实践/易于实现.
到目前为止,我正在考虑让网站的所有网址指向一个特定的PHP文件(比方说index.php),该文件将包含一个文件/ URL字典,用于将流量定向到正确的文件.
我真的不确定这是不是一个好方法......任何人都有更好的方法来做到这一点?我唯一想避免的是只在.htaccess中执行此操作...
我现在正在使用codeigniter.我目前能够通过使用来查看个人资料,/user/profile/profile_id但我希望能够让用户只需导航到个人资料/username以使其更简单.
我怎么会这样做我不知道从哪里开始?
class User extends CI_Controller{
public function index(){
if($this->session->userdata('is_logged_in')){
redirect('user/profile');
}
}
public function profile(){
$profile_id = $this->uri->segment(3);
$ip = $this->session->userdata('ip_address');
$curr_user = $this->session->userdata('id');
$data['profile'] = $this->db->get_where('users', array('id' => $profile_id))->row_array();
$data['followers'] = $this->db->get_where('followers', array('following_id' => $profile_id))->num_rows();
$data['following'] = $this->db->get_where('followers', array('follower_id' => $profile_id))->num_rows();
$data['doesFollow'] = $this->db->get_where('followers', array('follower_id' => $curr_user, 'following_id' => $profile_id))->num_rows();
$data['posts'] = $this->db->get_where('posts', array('user_id' => $profile_id))->result_array();
$data['main_content'] = 'profile';
$this->load->view('template', $data);
$this->get_profile_view($profile_id, $ip, $curr_user);
}
}
Run Code Online (Sandbox Code Playgroud)
routes.php文件
$route['default_controller'] = "signin";
$route['404_override'] = '';
Run Code Online (Sandbox Code Playgroud) 我正在使用高级模板。我在SiteController上创建了所有操作,因此所有URL都是domain.com/site/something,并且需要从URL中删除“ site”一词,因此它将是domain.com/something。
我根据这个问题尝试了以下规则
'urlManager' => [
'class' => 'yii\web\UrlManager',
'showScriptName' => false,
'enablePrettyUrl' => true,
'rules' => array(
'/<action:\w+>/<id:\d+>' => 'site/<action>',
'/<action:\w+>' => 'site/<action>',
'/noticia/<slug>' => 'site/noticia',
),
],
Run Code Online (Sandbox Code Playgroud)
也基于另一个问题尝试了此操作:
'urlManager' => [
'class' => 'yii\web\UrlManager',
'showScriptName' => false,
'enablePrettyUrl' => true,
'baseUrl' => 'http://localhost/websites/transcita/app/frontend/web',
'rules' => array(
[
'pattern' => '<action:\w+>',
'route' => 'site/<action>'
],
[
'pattern' => '<action:\w+>/<id:\d+>',
'route' => 'site/<action>'
],
'/noticia/<slug>' => 'site/noticia',
),
],
Run Code Online (Sandbox Code Playgroud)
但两者都不起作用。输入domain.com/something时得到404。我也尝试了没有第一个/的情况,但也不起作用。
有什么想法吗?
model-view-controller friendly-url pretty-urls yii2 yii2-advanced-app
friendly-url ×10
.htaccess ×3
mod-rewrite ×3
apache ×2
php ×2
url ×2
username ×2
asp.net-mvc ×1
codeigniter ×1
django ×1
java ×1
pretty-urls ×1
redirect ×1
seo ×1
uri ×1
yii2 ×1