Htaccess with URL parameters - order - php

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.

Related

.htaccess failing with RewriteBase

I have an htaccess file that
Takes care of making for example example.com/fr behave as example.com/index.php?lang=fr
Handles the blog:
--
RewriteEngine On
RewriteCond %{ENV:REDIRECT_rewritten} !=1
RewriteCond %{QUERY_STRING} ^lang=([a-z]{2})$ [NC]
RewriteRule ^([^/]*)$ /%1/$1? [R=301,L]
RewriteCond %{REQUEST_URI} !^/js/
RewriteRule ^([a-z]{2})(?:/([^/]+))?$ $2?lang=$1 [NC,L,QSA,E=rewritten:1]
#If the following part is removed, it works fine
RewriteBase /blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
How to fix this?
RewriteBase isn't even needed here because that is applicable for relative paths in .htaccess.
You should keep these rules in site root .htaccess:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+[^?]*\?lang=([^\s&]+)\s [NC]
RewriteRule ^ /%2/%1? [R=301,L,NE]
RewriteCond %{REQUEST_URI} !^/js/
RewriteRule ^([a-z]{2})(?:/([^/]+))?$ $2?lang=$1 [NC,L,QSA]
And create blog/.htaccess and keep these rules in blog/.htaccess:
DirectoryIndex index.php
RewriteEngine On
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]

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]

Redirect in based on cookie and prevent loop

I am trying to redirect if cookie is not set with htaccess, but I can't prevent the redirection loop.
It's for wordpress installation.
Here is my .htaccess at the moment:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_COOKIE} !^cookie_name[NC]
RewriteCond %{REQUEST_URI} !^/page_to_redirect/ [NC]
RewriteRule .* /page_to_redirect/ [R,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
How can I do that?
This works for me - if the cookie is set, the request isn't a file or directory, the request will be handled by index.php, otherwise it will be handled by redirect.php, which should presumably set the cookie.
RewriteEngine On
RewriteCond %{HTTP_COOKIE} cookie_name [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteRule .* /redirect.php [L]

controller and method translate in htaccess mod rewrite

I have some URL:
index.php?controller=user&method=show&id=7
in this URL: controller,method and show, are my keys
user, show and 7 are some kind of value
I would like translate URL to:
index.php?user/show/7
Is there any posibilities to translate this URL in .htaccess file?
Now, my .htaccess:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /Cobbler/index.php
I don't see the benefit of this but you can replace your current code by this one
RewriteEngine on
RewriteBase /Cobbler/
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule . ./ [R=301,L,QSA]
RewriteCond %{QUERY_STRING} ^([^/]+)/([^/]+)/([^/]+)$
RewriteRule ^$ index.php?controller=%1&method=%2&id=%3 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]

Mod_rewrite, with exception that refers to another rule

i want to use mod_rewrite on my page and already have some rules
now i want to redirect to artist.php?name=(PARAMETER) except it matches another rule
at the moment i use a "a/" before artist.php because i dont know how to solve this problem
so if i want so see the artist "Alex" ist just have to type domain.com/Alex and if i want to go to the about page i just have to type domain.com/about (just like facebook or twitter)
of course i'm thankfully for each improvement
Here is the .htaccess
RewriteEngine On
RewriteBase /
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^cover/([0-9]*)(s|m)?\.jpg$ /cover.php?id=$1&size=$2
RewriteRule ^dl/([^/]*)/([^/]*)$ /download.php?$1=$2
RewriteRule ^track/([^/]*)\.mp3$ /song.php?id=$1
RewriteRule ^my(/)([^/]*)$ /my.php?requested=$2
RewriteRule ^album/(.*)-([0-9]{0,6})$ /album.php?id=$2
RewriteRule ^a/([^/]*)$ /artist.php?name=$1
RewriteRule ^a/([^/]*)/music$ /music.php?name=$1
RewriteRule ^(artists|about|charts|contact|myprofile|editprofile|mymusic|uploadsongs|logout|genres)$ /$1.php
RewriteRule ^t$ /t.php
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?domain.com(/)?.*$ [NC]
RewriteRule .*\.(mp3)$ - [F,NC]
ErrorDocument 404 /errors/404.php
p.s.: i'm sorry for my bad english
You need to rearrange some of your rules, and add L flags so they don't accidentally get applied:
RewriteEngine On
RewriteBase /
RewriteRule ^cover/([0-9]*)(s|m)?\.jpg$ /cover.php?id=$1&size=$2 [L]
RewriteRule ^dl/([^/]*)/([^/]*)$ /download.php?$1=$2 [L]
RewriteRule ^track/([^/]*)\.mp3$ /song.php?id=$1 [L]
RewriteRule ^my(/)([^/]*)$ /my.php?requested=$2 [L]
RewriteRule ^album/(.*)-([0-9]{0,6})$ /album.php?id=$2 [L]
RewriteRule ^a/([^/]*)$ /artist.php?name=$1 [L]
RewriteRule ^a/([^/]*)/music$ /music.php?name=$1 [L]
RewriteRule ^(artists|about|charts|contact|myprofile|editprofile|mymusic|uploadsongs|logout|genres|t)$ /$1.php [L]
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?domain.com(/)?.*$ [NC]
RewriteRule .*\.(mp3)$ - [F,NC]
# assume anything else is a username
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ /artist.php?name=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/music$ /music.php?name=$1 [L]
ErrorDocument 404 /errors/404.php

Categories