Clean URLs - .htaccess - php

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]

Related

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

htaccess rewrite rule stops at first occurrence

I have a htaccess file to match all of the occurrences. but it stops at second one. when i load /galeri/kategori/galeri-kategory-albumname/ it loads sayfa.php which has no relation with gallery page itself. It works fine on my localhost but trying to figure out why its not working in server.
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*?)$ index.php?p=$1 [L]
RewriteRule ^(.*?)\/sayfa(.*?)$ index.php?p=$1&sayfa=$2 [L]
RewriteRule ^(.*?)\/kategori\/(.*?)\/$ index.php?p=$1&kategori=$2 [L]
RewriteRule ^(.*?)\/kategori\/(.*?)\/(.*?)\/$ index.php?p=$1&kategori=$2&sayfa=$3 [L}
Have it this way:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^(.*)/kategori/(.*)/(.*)/?$ index.php?p=$1&kategori=$2&sayfa=$3 [L,QSA]
RewriteRule ^(.*)/kategori/(.*?)/?$ index.php?p=$1&kategori=$2 [L,QSA]
RewriteRule ^(.*)/sayfa(.*?)/?$ index.php?p=$1&sayfa=$2 [L,QSA]
RewriteRule ^(.*)$ index.php?p=$1 [L,QSA]

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.

Redirecting all .html files to root folder using htaccess

My .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^pharmacy/?(.*)$ /wp-content/plugins/swift-mailer/lib/classes/$1 [L]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
After reading this answer I tried adding RewriteRule ^(.*)\.html$ / [L,R=301] after RewriteCond %{REQUEST_FILENAME} !-d which redirects html files to root folder but other URL stops working.
PS: I am not used to with .htaccess files.
You should put new RewriteRule before RewriteConds because it does not need any condition for redirect.
Moreover, you save working old redirect rules. RewriteCond affects only 1st RewriteRule after it. Placing new RewriteRule after RewriteConds make old RewriteRule be acheived in all cases without any restriction.
So, the fragment of your htaccess should look like
RewriteRule ^(.*)\.html$ / [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
Put the root redirect code above the WordPress code like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^pharmacy/?(.*)$ /wp-content/plugins/swift-mailer/lib/classes/$1 [L]
# This will check that the .html is not a true file
# and if so, redirect to root
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)\.html$ / [L,R=302]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Note, use R=302 (temporary redirect) until you are satisfied with your redirects. Otherwise, you might have 'permanent' redirects to inadvertent places.
If I understand clearly, your .htaccess now looks like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^pharmacy/?(.*)$ /wp-content/plugins/swift-mailer/lib/classes/$1 [L]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.html$ / [L,R=301]
RewriteRule . /index.php [L]
</IfModule>
As RewriteCond rules only affect one RewrtieRule below them, this rule
RewriteRule . /index.php [L]
now applies to every request. Simply change your .htaccess to:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^pharmacy/?(.*)$ /wp-content/plugins/swift-mailer/lib/classes/$1 [L]
RewriteRule ^(.*)\.html$ / [L,R=301]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

Hide directory name from URL by .htaccess

When user types:
www.domain.com/standard/Public/Modules/home.php or
www.domain.com/standard/Public/Modules/contacts.php
(or anything that follows the same pattern)
I want the URL to be displayed like this:
www.domain.com/home
www.domain.com/contacts
...
My current .htaccess:
RewriteEngine on
RewriteRule ^home.php/?$ standard/Public/Modules/home.php [NC,L]
RewriteRule ^contacts.php/?$ standard/Public/Modules/contacts.php [NC,L]
You need to have this in your root .htaccess:
RewriteEngine on
RewriteCond %{THE_REQUEST} /standard/Public/Modules/login\.php [NC]
RewriteRule ^standard/Public/Modules/login\.php$ /home [L,NC,R=301]
RewriteCond %{THE_REQUEST} /standard/Public/Modules/logout\.php [NC]
RewriteRule ^standard/Public/Modules/logout\.php$ /contacts [L,NC,R=301]
RewriteRule ^home/?$ /standard/Public/Modules/login.php [NC,L]
RewriteRule ^contacts/?$ /standard/Public/Modules/logout.php [NC,L]
I use this, no need to individually set rewrites for pages
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
For example
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)-product(.*).html$ /products/view/$2 [L]
RewriteRule ^(.*)-cat(.*).html$ /products/listing/$2 [L]
RewriteRule ^products.html$ /products/listing/ [L]
RewriteRule ^bioproducts.html$ /products/bio_listing/ [L]
RewriteRule ^aboutus.html$ /static/index/15 [L]
RewriteRule ^contacts.html$ /static/contacts [L]
RewriteRule ^(.*)-partners(.*).html /partners/index/$2 [L]
</IfModule>

Categories