简体   繁体   English

htaccess漂亮的URL头

[英]htaccess for pretty url slugs

I am trying to set up a .htaccess file in the root of my project which will give me the ability to use pretty urls. 我正在尝试在项目的根目录中建立一个.htaccess文件,这将使我能够使用漂亮的url。

I have three types of page: 我有三种类型的页面:

  1. news.php news.php
  2. tournaments.php 锦标赛
  3. rankings.php ranks.php

However, in reality I want news.php and view.php in which the latter will handle which mark up to use for tournaments / rankings and news stories will be site-name/news/the-slug-of-the-story/ 但是,实际上,我需要news.phpview.php ,后者将在其中处理用于比赛/排名的标记,而新闻报道将是site-name/news/the-slug-of-the-story/

Here is what I have so far: 这是我到目前为止的内容:

RewriteEngine On
RewriteBase /the-site-name/
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule . /news.php [L]
RewriteRule ^news/(.*)/([0-9]+) news.php?slug=/news/$1 [L]
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule . /view.php [L]
RewriteRule ^tournaments/(.*)/([0-9]+) view.php?page=/tournaments/$1 [L]
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule . /view.php [L]
RewriteRule ^rankings/(.*)/([0-9]+) view.php?page=/rankings/$1 [L]

However, all of the links /tournaments/ /rankings/ seem to be using the news.php template and not view.php. 但是,所有链接/ tournaments / / rankings /似乎都在使用news.php模板,而不是view.php。

Is there anything I am missing? 我有什么想念的吗?

UPDATED: 13:44pm 20/02/2018 更新时间:2018年2月13日下午13:44

I have the .htaccess working now - this is final set up 我现在可以使用.htaccess了-这是最终设置

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^news/?$ news.php?slug=/news/ [L]
RewriteRule ^news/(.*)/([0-9]*) news.php?slug=/news/$1/ [L]
RewriteRule ^rankings/?$ view.php?page=/rankings/ [L]
RewriteRule ^tournaments/?$ view.php?page=/tournaments/ [L]
</IfModule>

The only bug now is /news/name-of-story/ works but without the final / it will break if one visits /news/name-of-story . 现在唯一的错误是/news/name-of-story/可以正常运行,但没有最终版本/如果有人访问/news/name-of-story ,它将断开。

I will be surprised if this work on my digital ocean in the same way as XAMPP but I will cross that bridge when I come to it =] 如果这项工作能以与XAMPP相同的方式在我的数字海洋上工作,我会感到惊讶,但是当我到达它时,我将跨越那座桥=]

UPDATE: 13:51PM 更新:13:51 PM

I also added 我还加了

RewriteRule ^news/(.*) news.php?slug=/news/$1/ [L]

to handle the lack of trailing slash 处理缺少斜杠的问题

[L] means if the rule matches, don't process any more RewriteRules below this one. [L]表示如果规则匹配,则不要再处理低于此规则的RewriteRules。

Untested but, should be : 未经测试,但应为:

RewriteEngine On
RewriteBase /the-site-name/
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^news/(.*)/([0-9]+) news.php?slug=/news/$1 [L]
RewriteRule ^tournaments/(.*)/([0-9]+) view.php?page=/tournaments/$1 [L]
RewriteRule ^rankings/(.*)/([0-9]+) view.php?page=/rankings/$1 [L]

update : (tested) -- NB : Added /$2 (may not what you want, could remove it) 更新:(已测试)-注意:已添加/$2 (可能不是您想要的,可以将其删除)

RewriteEngine On
RewriteBase /the-site-name/
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^news/?$ news.php?slug=/news/ [L]
RewriteRule ^news/(.*)/?([0-9]*) news.php?slug=/news/$1/$2 [L]
RewriteRule ^rankings/?$ view.php?page=/rankings/ [L]
RewriteRule ^rankings/(.*)/([0-9]+) view.php?page=/rankings/$1/$2 [L]
RewriteRule ^tournaments/?$ view.php?page=/tournaments/ [L]
RewriteRule ^tournaments/(.*)/([0-9]+) view.php?page=/tournaments/$1/$2 [L]

Explanation : 说明:

^news/?$   # For "news" or "news/"
^news/(.*)/([0-9]+) news.php?slug=/news/$1/$2 # Added $2 for the second capture group

I can't test this at the moment, but from what I can remember, these lines will cause that: 我目前无法对此进行测试,但是据我所记得,这些行将导致以下情况:

RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule . /news.php [L]

That . 那个. is basically saying 'rewrite all paths that aren't a file or directory to news.php'. 基本上是说“将不是文件或目录的所有路径都重写到news.php”。

RewriteRule works like so: RewriteRule工作原理如下:

RewriteRule Pattern Substitution

The pattern . 模式. means anything and could be causing the problem. 意味着任何东西,可能会导致问题。

Try taking out these lines: 尝试取出这些行:

RewriteRule . /news.php [L]
RewriteRule . /view.php [L]
RewriteRule . /view.php [L]

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

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