简体   繁体   中英

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.

I have three types of page:

  1. news.php
  2. tournaments.php
  3. rankings.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/

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.

Is there anything I am missing?

UPDATED: 13:44pm 20/02/2018

I have the .htaccess working now - this is final set up

<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 .

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 =]

UPDATE: 13:51PM

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.

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)

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'.

RewriteRule works like so:

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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