htaccess for pretty url slugs - php

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:
news.php
tournaments.php
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]

Related

Clean URLs - .htaccess

Clean URLs works only on index.php but not on the other pages (explore and top).
The parameter I want to "clean" is 'genre' (for example: genre=pop). This is the website:swiftlymusic.com and is hosted on a "1&1(1and1 oneandone)" server.
.htaccess:
ErrorDocument 404 /04.html
RewriteBase /
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^log$ logs [L]
RewriteRule ^([a-zA-Z0-9]+)$ ?genre=$1 [L]
RewriteRule ^([a-zA-Z0-9]+)/$ ?genre=$1 [L]
RewriteRule ^explore/([a-zA-Z0-9_-]+)$ explore.php?genre=$1 [L]
RewriteRule ^top/([a-zA-Z0-9_-]+)$ top.php?genre=$1 [L]
AddDefaultCharset UTF-8
Try it like this,
Options -Multiviews
ErrorDocument 404 /04.html
RewriteBase /
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^log$ logs [L]
RewriteRule ^([\w-]+)$ ?genre=$1 [L]
RewriteRule ^([\w-]+)/$ ?genre=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#explore & top and any other can be use by this 1 rule
RewriteRule ^([\w]+)/([\w-]+)$ $1.php?genre=$2 [QSA,L]
Have you tried re-ordering so that the "explore.php" and "top.php" lines are above the general line? Also, you can use '?' instead of repeating the line twice: once with, once without the forward slash.
RewriteRule ^explore/([a-zA-Z0-9_\-]+)/?$ explore\.php?genre=$1 [L]
RewriteRule ^top/([a-zA-Z0-9_\-]+)/?$ top\.php?genre=$1 [L]
RewriteRule ^([a-zA-Z0-9]+)/?$ ?genre=$1 [L]

Htaccess with URL parameters - order

I've two patterns that need to be redirected.
www.example.com/tag/value to www.example.com/recipe-tag/value
www.example.com/?tag=value to www.example.com/recipe-tag/value
This is my current .htaccess
RewriteEngine On
RewriteBase /
RewriteRule ^tag/(.*) http://www.example.com/recipe-tag/$1 [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
This works for the first redirect. What should I do to get the second redirect working?
You have to use a condition to detect query string. Give this a try.
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} tag=(.+)$
RewriteRule ^ http://www.example.com/recipe-tag/%1? [R=301,L]
RewriteRule ^tag/(.*) http://www.example.com/recipe-tag/$1 [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
Let me know how that works for you.

Adding a third Mod Rewrite - mod_rewrite

guys. I'm trying to add a third rewrite condition in my .htaccess. The problem is, I have no idea how to add another one. So if anyone has a good in-depth tutorial on it I would be greatful.
Anyway back to business. Here is my current code:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ users.php?name=$1 [QSA,L]
The above as you might see removes the .php extensions and makes all files that don't exist go to users(IE: website.com/user goes to user's profile page).
So now I need to add one for the forum. I would like it rewrite like "website.com/Group/Category/Thread".
Is this even possible, or do I need to lower my expectations for this? Any help would be greatly appreciated. Thanks :D
Hmm I remember posting this answer :)
Anyway you can create a new rule on top of previous rules like this (and replace /Group/Category/Thread with whatever you have) :
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^forum/?$ /Group/Category/Thread [L,NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ users.php?name=$1 [QSA,L]
You can insert more rules. Given that you want these rules to be matched before your final catch-all to user.php, you should place them before that final rule.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
# match forum, forum/group, forum/group/category and forum/group/category/thread urls
RewriteRule ^forum/?$ forum.php?group=$1&category=$2&thread=$3 [L]
RewriteRule ^forum/([^/]+)/?$ forum.php?group=$1 [L]
RewriteRule ^forum/([^/]+)/([^/]+)/?$ forum.php?group=$1&category=$2 [L]
RewriteRule ^forum/([^/]+)/([^/]+)/([^/]+)/?$ forum.php?group=$1&category=$2&thread=$3 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ users.php?name=$1 [QSA,L]

.htaccess file extension and $_GET rewrite

i want to rewite these in my .htacces file:
/*.php -> /*(/)
(e.g. gallery.php to /gallery or /gallery/)
/snippets.php*?s= -> /snippets/*
(e.g. snippets.php*?s=test to /snippets/test or /snippets/test/)
my code so far:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [L]
RewriteRule ^snippets/([^/\.]+)/?$ snippets.php?s=$1 [L]
the bugs that appear using my code:
/snippets/ and /snippets/test(/) will alert an 500 Error. /snippets works fine.
what am i doing wrong?
Like Micheal said, you need to change the order, however Michael didn't move the RewriteCond's, which result in the unexpected behavior.
RewriteEngine on
RewriteBase /
RewriteRule ^snippets/([^/.]+)/?$ snippets.php?s=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ $1.php [L]
I verified this code on my test server, just to be sure.
You almost have this correct. To take specific action for /snippets, it will need to come before the catch-all rule. Otherwise the first rule matches and routes to snippets/test.php which doesn't exist.
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Match snippets first...
RewriteRule ^snippets/([^/.]+)/?$ snippets.php?s=$1 [L]
# Then the catch-all for remaining matches
RewriteRule ^(.*)$ $1.php [L]

Redirect two files with htaccess

this is my scenario. My application urls are showed like this:
example.com/index.php/article/whatever/here
in order to remove the index.php from url I have put this in my .htaccess and works fine:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
Now, my problem is that I have include and admin.php like this:
example.com/admin.php/manage/whatever/here
And I don't know how to remove the admin.php from url without make conflics with the index.php because I have tried this and a 500 Internal Server Error is showed:
RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteRule ^(.*)$ /admin.php/$1 [L]
EDIT
Another try was this and nothing, because a 404 error is showed:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/(.*?)/(.*) /$1/$2 [L]
ALL my urls should have several parameteres as requiered:
example.com/this/is/a/large/url/because/is/requiered
EDIT 2:
I have create two rules, each rule alone works fine!!! but both can't work together:
RewriteRule ^admin/(.*)$ /admin.php/$1 [L]
RewriteRule ^(.*)$ /index.php/$1 [L]
Try:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond ${REQUEST_URI} ^admin/(.*)$ [NC]
RewriteRule ^(.*)$ /admin.php/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
Then at least test this with:
http://www.example.com/some/url
http://www.example.com/admin/some/url
You can slightly modify your rewrite rule to capture the first part of the path, and use it in the replacement. Try this:
RewriteRule ^/(.*?)/(.*) /$1/$2 [L]
Basically the () create a capturing group, so anything between the first two / will be captured as $1 and anything after that will be captured as $2
Note that you can also change your rewrite conditions, if you only want this replacement to apply to index.php and admin.php

Categories