what is wrong with this htaccess multi level directory rewrite rule? - .htaccess

I have some problems with my htaccess...
I want to have my url's like this:
http://example.com/artist/
http://example.com/artist/rihanna/
http://example.com/artist/rihanna/biography/
http://example.com/artist/rihanna/video/
http://example.com/artist/rihanna/news/
The problem is all of the url's work except for "http://example.com/artist/"
RewriteRule ^artist/([^_]*)/biography/$ /artist-biography.php?name=$1 [L]
RewriteRule ^artist/([^_]*)/biography?$ /artist/$1/biography/ [R=301,L]
RewriteRule ^artist/([^_]*)/video/$ /artist-video.php?name=$1 [L]
RewriteRule ^artist/([^_]*)/video?$ /artist/$1/video/ [R=301,L]
RewriteRule ^artist/([^_]*)/news/$ /artist-news.php?name=$1 [L]
RewriteRule ^artist/([^_]*)/news?$ /artist/$1/news/ [R=301,L]
RewriteRule ^artist/([^_]*)/$ /artist.php?name=$1 [L]
RewriteRule ^artist/([^_/]+)$ /artist/$1/ [R=301,L]
RewriteRule ^artist/$ /artist-page.php [L]
RewriteRule ^artist?$ /artist/ [R=301,L]

This line
RewriteRule ^artist/([^_]*)?$ /artist/$1/ [R=301,L]
will match http://example.com/artist/ which is probably not what you wanted. Change it as below
RewriteRule ^artist/([^_/]+)$ /artist/$1/ [R=301,L]
If that does not fix it completely, let me know the result.

Related

httaccess, all rewrite is redirecting to index.php

Hi i have a some problem with .htaccess, I need make url structure like this: https://example.com/folder/name1
folder - is directory with all files, .htaccess, indexes etc..
name1 - is parameter for backend, mysql etc...
In my .htaccess, I have this code:
RewriteRule ^(.*)/shop shop.php
RewriteRule ^(.*)/voucher/ voucher.php
RewriteRule ^(.*)/voucher voucher.php
RewriteRule ^(.*)/rules/ rules.php
RewriteRule ^(.*)/rules rules.php
RewriteRule ^(.*)/item/(.*)/ item.php?id=$2
RewriteRule ^(.*)/item/(.*) item.php?id=$2
RewriteRule ^(.*)/item/ item.php
RewriteRule ^(.*)/item item.php
RewriteRule ^(.*)/ index.php
RewriteRule ^(.*) index.php
And problem is with two last lines, because when I go to https://example.com/folder/name1 is working, I seed index.php file, but when I go https://example.com/folder/name1/shop I still see index.php. Where is problem?
All of your rewrite rules should have the "last" flag on them ([L]) which tells mod_rewrite not to process other rules after. What is happening is this your last rule matches all requests, even ones that have been previously rewritten.
RewriteRule ^(.*)/shop shop.php [L]
RewriteRule ^(.*)/voucher/ voucher.php [L]
RewriteRule ^(.*)/voucher voucher.php [L]
RewriteRule ^(.*)/rules/ rules.php [L]
RewriteRule ^(.*)/rules rules.php [L]
RewriteRule ^(.*)/item/(.*)/ item.php?id=$2 [L]
RewriteRule ^(.*)/item/(.*) item.php?id=$2 [L]
RewriteRule ^(.*)/item/ item.php [L]
RewriteRule ^(.*)/item item.php [L]
RewriteRule ^(.*)/ index.php [L]
RewriteRule ^(.*) index.php [L]

Htaccess add and force trailing slash

I am having some trouble with my htaccess code:
RewriteEngine on
RewriteRule ^about\/$ /about.php [L]
RewriteRule ^about?$ /about/ [L,R]
RewriteRule ^contact\/$ /contact.php [L]
RewriteRule ^contact?$ /contact/ [L,R]
RewriteRule ^([A-Za-z0-9\-]+)\/gallery\/$ /loc-gallery.php?slug=$1 [L]
RewriteRule ^([A-Za-z0-9\-]+)\/gallery?$ /$1/gallery/ [L,R]
RewriteRule ^([A-Za-z0-9\-]+)\/location\/$ /loc-location.php?slug=$1 [L]
RewriteRule ^([A-Za-z0-9\-]+)\/location?$ /$1/location/ [L,R]
RewriteRule ^([A-Za-z0-9\-]+)\/contact\/$ /loc-contact.php?slug=$1 [L]
RewriteRule ^([A-Za-z0-9\-]+)\/contact?$ /$1/contact/ [L,R]
RewriteRule ^([A-Za-z0-9\-]+)\/$ /loc-home.php?slug=$1 [L]
RewriteRule ^([A-Za-z0-9\-]+)?$ /$1/ [L,R]
The problem is that if i want to access:
http://example.com/about
http://example.com/contact
i am not redirected to
`http://example.com/about/` and `http://example.com/contact/`
and when i try to access http://example.com/ i am redirected to http://example.com//
What i am doing wrong? Thanks in advance!
When you try to access http://example.com/ i am redirected to http://example.com// is happening because of this line :
RewriteRule ^([A-Za-z0-9\-]+)?$ /$1/ [L,R]
It will match the empty URI because of this regex ^([A-Za-z0-9\-]+)?$
it means match if ([A-Za-z0-9\-]+) then or not ? .
It should look like this :
RewriteRule ^([A-Za-z0-9\-]+)$ /$1/ [L,R]
I also sumerized your code and fixed some issues like this :
RewriteEngine on
RewriteRule ^about(\/)?$ /about.php [L]
RewriteRule ^contact(\/)?$ /contact.php [L]
RewriteRule ^([A-Za-z0-9\-]+)\/gallery(\/)?$ /loc-gallery.php?slug=$1 [L]
RewriteRule ^([A-Za-z0-9\-]+)\/location(\/)?$ /loc-location.php?slug=$1 [L]
RewriteRule ^([A-Za-z0-9\-]+)\/contact(\/)?$ /loc-contact.php?slug=$1 [L]
RewriteRule ^([A-Za-z0-9\-]+)\/$ /loc-home.php?slug=$1 [L]
RewriteRule ^([A-Za-z0-9\-]+)$ /$1/ [L,R]
Clear browser cache and test it , if it is ok change R to R=301 to get permanent redirection

Ensure rewrite-rule is used

I have created a simple rewrite URL like this:
RewriteRule ^messages$ /messages.php [L]
How do I force the user to use /messages and not /messages.php? I.e., if user queries messages.php, he'll be 301 redirected to /messages.
I have a long list of rewrite rules, like the one above.
RewriteEngine On
RewriteRule ^messages$ /messages.php [L]
RewriteRule ^events$ /events.php [L]
RewriteRule ^cv$ /cv.php [L]
RewriteRule ^profile/([^/]*)$ /profile.php?id=$1 [L]
RewriteRule ^ratings$ /ratings.php [L]
RewriteRule ^newsfeed$ /newsfeed.php [L]
RewriteRule ^logout$ /logout.php [L]
RewriteRule ^profile$ /profile.php [L]
RewriteRule ^ranking$ /rank.php [L]
RewriteRule ^find-study$ /search.php [L]
RewriteRule ^search/$ /search.php [L]
RewriteRule ^search$ /search.php [L]
RewriteRule ^invite-friends$ /invite-friends.php [L]
RewriteRule ^profile/([^/]*)$ /profile.php?id=$1 [L]
RewriteRule ^profile/([^/]*)/([^/]*)$ /profile.php?id=$1&programme=$2 [L]
RewriteRule ^student-requests$ /student-requests.php [L]
# Q&A
# view question
RewriteRule ^question(?:/([^/]+)(?:/([^/]*))?)?/?$ /question.php?id=$1&permalink=$2 [L]
RewriteRule ^question/([^/]*)/([^/]*)/([^/]*)$ /question.php?id=$1&permalink=$2&answer=$3 [L]
# manage question
RewriteRule ^ask(?:/([^/]+)(?:/([^/]*))?)?/?$ /manageQuestion.php?tag_id=$1&tag_type=$2 [L]
RewriteRule ^ask/([^/]*)/([^/]*)/([^/]*)$ /manageQuestion.php?tag_id=$1&tag_type=$2&tag_name=$3 [L]
RewriteRule ^editquestion(?:/([^/]+)(?:/([^/]*))?)?/?$ /manageQuestion.php?id=$1&second=$2 [L]
# questions
RewriteRule ^questions$ /questions.php [L]
RewriteRule ^questions/([^/]*)$ /questions.php?first=$1 [L]
RewriteRule ^questions/([^/]*)/([^/]*)$ /questions.php?first=$1&second=$2 [L]
RewriteRule ^questions/([^/]*)/([^/]*)/([^/]*)$ /questions.php?first=$1&second=$2&third=$3 [L]
RewriteRule ^questions/([^/]*)/([^/]*)/([^/]*)/([^/]*)$ /questions.php?first=$1&second=$2&third=$3&fourth=$4 [L]
When trying to solve this problem you probably encountered the problem of infinite loops. One way to prevent this is to use %{THE_REQUEST}, since this variable doesn't change on a rewrite. Therefore, if you use the following rule, it will change every url ending on .php to an url without it:
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /(messages|events|cv|ratings|etc)\.php\ HTTP
RewriteRule ^ %2 [R,L]
After testing that the rule works correctly, change [R,L] to [R=301,L] to make the redirect permanent. Doing this before everything works correctly makes testing a pain.
Edit: Instead of using (.*) you could use /(page1|page2|page3) for all the simple redirects. For each of the more complex ones you probably need to have a single redirect each in the form of:
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /questions\.php\?id=([^&]+)&permalink=([^&]+)&answer=([^&]+)\ HTTP
RewriteRule ^ questions/%2/%3/%4 [R,L]

creating a htaccess redirect that includes a special character

I need to create a 301 redirect within my htaccess file, the problem is the links have the (?) special character:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^news/news-item?blue-spares-appoints-stuart-truckel-as-sales-director?$ http://www.example.com/news [R=301,L]
RewriteRule ^news/news-item?hugely-successful-scot-plant-for-blue-scotland?$ http://www.example.com/news [R=301,L]
RewriteRule ^news/news-item?port-of-workington-takes-specialist-fuchs-materials-handler?$ http://www.example.com/news [R=301,L]
RewriteRule ^news/news-item?blue-spares-go-green-with-new-shredder?$ http://www.example.com/news [R=301,L]
RewriteRule ^used-machinery/en/shredders/used-machinery-item?000739:beast-bandit-2860?$ http://www.example.com/machinery [R=301,L]
RewriteRule ^about-us/careers/careers-item?project-manager-blue-machinery-london-ltd http://www.example.com/about [R=301,L]
RewriteRule ^news/news-item?thompsons-plant-hire-continues-allegiance-to-doppstadt-and-blue-machinery http:// http://www.example.com/news [R=301,L]
RewriteRule ^used-machinery/en/finlay/used-machinery-item?000549:798-double-deck-skin-trommel-screen http://www.example.com/machinery [R=301,L]
RewriteRule ^used-machinery/en/screeners/used-machinery-item?000549:798-double-deck-skin-trommel-screen http://www.example.com/machinery [R=301,L]
RewriteRule ^product-videos/shredders/doppstadt-ak-range/ http://www.example.com/product-videos [R=301,L]
RewriteRule ^product-videos/shredders/doppstadt-ak-range/?1 http://www.example.com/product-videos [R=301,L]
RewriteRule ^product-videos/shredders/doppstadt-ak-range/?4 http://www.example.com/product-videos [R=301,L]
RewriteRule ^product-videos/shredders/doppstadt-ak-range/?3 http://www.example.com/product-videos [R=301,L]
RewriteRule ^product-videos/shredders/doppstadt-ak-range/?7 http://www.example.com/product-videos [R=301,L]
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
What would be the best way to redirect each of these urls to their correct destinations?
All characters after ? sign is part of QUERY_STRING, you must add conditions to query string before rules:
RewriteCond %{QUERY_STRING} ^[0-9a-z\-\:]+$
RewriteRule ^product-videos/shredders/doppstadt-ak-range/? http://www.example.com/product-videos [R=301,L]
RewriteRule ^news/news-item http://www.example.com/news [R=301,L]
RewriteRule ^used-machinery/en/shredders/used-machinery-item$ http://www.example.com/machinery [R=301,L]
RewriteRule ^about-us/careers/careers-item$ http://www.example.com/about [R=301,L]
RewriteRule ^news/news-item$ http:// http://www.example.com/news [R=301,L]
RewriteRule ^used-machinery/en/finlay/used-machinery-item http://www.example.com/machinery [R=301,L]
RewriteRule ^used-machinery/en/screeners/used-machinery-item http://www.example.com/machinery [R=301,L]
The adobe rules will match:
news/news-item?blue-spares-appoints-stuart-truckel-as-sales-director
news/news-item?hugely-successful-scot-plant-for-blue-scotland
news/news-item?port-of-workington-takes-specialist-fuchs-materials-handler
news/news-item?blue-spares-go-green-with-new-shredder
product-videos/shredders/doppstadt-ak-range/?1
product-videos/shredders/doppstadt-ak-range/?4
product-videos/shredders/doppstadt-ak-range/?3
product-videos/shredders/doppstadt-ak-range/?7
And other URLs
See RewriteCond documentation:
http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html#RewriteCond

mod rewrite right markup

I have mod rewrite expressions in .htaccess
RewriteRule ^/?(am|ru|en)/([a-z]+|[0-9]+)/([a-z0-9]+)$ /index.php?lang=$1&cat=$2&ac=$3 [L]
RewriteRule ^/?([0-9]+)/(am|ru|en)/([a-z]+|[0-9]+)/([a-z0-9]+)$ /index.php?start=$1&lang=$2&cat=$3&ac=$4 [L]
RewriteRule ^/?(am|ru|en)/([a-z]+|[0-9]+)/([a-z0-9]+)$ /index.php?lang=$1&cat=$2&bc=$3 [L]
RewriteRule ^/?([0-9]+)/(am|ru|en)/([a-z]+|[0-9]+)/([a-z0-9]+)$ /index.php?start=$1&lang=$2&cat=$3&bc=$4 [L]
RewriteRule ^/?(am|ru|en)/([a-z]+|[0-9]+)/([a-z0-9]+)$ /index.php?lang=$1&cat=$2&cc=$3 [L]
RewriteRule ^/?([0-9]+)/(am|ru|en)/([a-z]+|[0-9]+)/([a-z0-9]+)$ /index.php?start=$1&lang=$2&cat=$3&cc=$4 [L]
but GET's of links are wrong because of same (repeating) conditions (for ac, bc, cc).
I have this kind of solution, but don't like this:
RewriteRule ^/?(am|ru|en)/([a-z]+|[0-9]+)/([x]+[0-9a-z]+)$ /index.php?lang=$1&cat=$2&ac=$3 [L]
RewriteRule ^/?([0-9]+)/(am|ru|en)/([a-z]+|[0-9]+)/([x]+[0-9a-z]+)$ /index.php?start=$1&lang=$2&cat=$3&ac=$4 [L]
RewriteRule ^/?(am|ru|en)/([a-z]+|[0-9]+)/([y]+[0-9a-z]+)$ /index.php?lang=$1&cat=$2&bc=$3 [L]
RewriteRule ^/?([0-9]+)/(am|ru|en)/([a-z]+|[0-9]+)/([y]+[0-9a-z]+)$ /index.php?start=$1&lang=$2&cat=$3&bc=$4 [L]
RewriteRule ^/?(am|ru|en)/([a-z]+|[0-9]+)/([z]+[0-9a-z]+)$ /index.php?lang=$1&cat=$2&cc=$3 [L]
RewriteRule ^/?([0-9]+)/(am|ru|en)/([a-z]+|[0-9]+)/([z]+[0-9a-z]+)$ /index.php?start=$1&lang=$2&cat=$3&cc=$4 [L]
What is right solution for this.

Resources