.htaccess mod_rewrite with 3 directories levels - regex

How can I manage 3 subdirectories levels on Htaccess?
Explanation:
http://example.com/category1/
http://example.com/category2/
http://example.com/category3/
RewriteRule to *category.php?category=$1*
http://example.com/category1/type1/
http://example.com/category2/type7/
http://example.com/category3/type8/
RewriteRule to *type.php?category=$1&type=$2*
http://example.com/category1/type1/company3/
http://example.com/category2/type7/company4/
http://example.com/category3/type8/company9/
RewriteRule to *company.php?category=$1&type=$2&company=$3*
.htaccess file is located in initial root folder (where index.php)
Is the next the best way?
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/.]+)(?:/)?$ /category.php?category=$1 [L]
RewriteRule ^([^/.]+)/([^/.]+)(?:/)?$ /type.php?category=$1&type=$2 [L]
RewriteRule ^([^/.]+)/([^/.]+)/([^/.]+)(?:/.*)?$ /company.php?category=$1&type=$2&company=$3 [L]

You are fairly close. Just note that RewriteCond is applicable to next immediate RewriteRule only so you check of !-f will only be used for first rule. You can have a separate rule to discard all requests that are for existing files and directories.
RewriteEngine On
# skip all files and directories from rewrite rules below
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^([\w-]+)/?$ category.php?category=$1 [L,QSA]
RewriteRule ^([\w-]+)/([\w-]+)/?$ type.php?category=$1&type=$2 [L,QSA]
RewriteRule ^([\w-]+)/([\w-]+)/([\w-]+)/?$ company.php?category=$1&type=$2&company=$3 [L,QSA]

Related

Regex pattern to exclude specific words at the start of url using htaccess

[Edited code]*
This is the current code I am using
Options +FollowSymLinks
RewriteCond %{REQUEST_URI} (bus/(.*))
RewriteRule bus/(.*) page1.php?param=$1 [L,NC,QSA]
RewriteCond %{REQUEST_URI} (car/(.*))
RewriteRule car/(.*) test/page2.php?param=$1 [L,NC,QSA]
RewriteCond %{REQUEST_URI} (bike/(.*))
RewriteRule bike/(.*) test/page3.php?param=$1 [L,NC,QSA]
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+)/(.+)$ new.php?param1=$1&param2=$2 [L,QSA]
I am trying to get the wild card condition in the last line if the above conditions fail. But the server returning with the 404. Its not catching the condition.
Any Help would be appreciated.
Keep all specific rules are top and 3rd generic catch-all rule at the last:
RewriteEngine On
RewriteRule ^bus/(.+)$ page1.php?param=$1 [L,NC,QSA]
RewriteRule ^car/(.+)$ test/page2.php?param=$1 [L,NC,QSA]
RewriteRule ^bike/(.+)$ test/page3.php?param=$1 [L,NC,QSA]
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+)/(.+)$ new.php?param1=$1&param2=$2 [L,QSA]
Two RewriteCond are required before last rule to prevent rewriting actual files and directories.
Your need to use the L flag at the end of your rules to stop multiple rule processing :
RewriteRule ^ask/(.*) forum.php?param=$1 [L,NC]
RewriteRule ^city/(.*) city.php?param=$1 [L,NC]

htaccess: multiple rewriterule

I want to rewrite the following:
http://www.example.com/map to http://www.example.com/index.php?p=map
and
http://www.example.com/map/1 to http://www.example.com/index.php?p=map&id=1
My current code is:
RewriteEngine On
RewriteRule ^([^/]*)\$ /index.php?p=$1 [NC]
RewriteRule ^([^/]*)/([^/]*)\$ /index.php?p=$1&id=$2 [L]
But if fails...
Who can help me with the right .htaccess code? Thanx in advance!
You need to ignore real files and directories from your rewrite and don't escape $.
You can use:
RewriteEngine On
# rule to ignore files and directories from all rewrite rules below
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^([^/]+)/?$ /index.php?p=$1 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)/?$ /index.php?p=$1&id=$2 [L,QSA]

HTACCESS With exception for certain folders

I have an old website i'm trying to take over, and it has a bad htaccess file here that I need to adjust to enable 'FOLDERS', instead of routing EVERYTHING.
So I have a folder called, "RESOURCES", but it's trying to route through the index file with it..... but I need to load the resource folder instead of routing. How do I make an exception?
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.website\.ca$ [NC]
RewriteRule .? http://www.website.ca%{REQUEST_URI} [R=301,L,QSA]
RewriteRule ^(browse)/?$ /browse.php [L,QSA]
RewriteRule ^([\ \+\-0-9a-zA-Z_]+)/?$ /index.php?where=$1 [L,QSA]
#RewriteRule ^([\ \+\-0-9a-zA-Z_]+)/([-0-9a-zA-Z_]+)/([-0-9a-zA-Z_]+)/?$ /index.php?where=$1&what=$2 [L,QSA]
RewriteRule ^(-0-9a-zA-Z_]+)/([-0-9a-zA-Z_]+)/?$ /index.php?where=$1&what=$2 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . / [R=301,L,QSA]
</IfModule>
Add this rule just below RewriteBase /
RewriteRule ^RESOURCES/- [L,NC]
This will skip rest of the rewrite rules for folder called RESOURCES
To skip all the folders use this rule:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^- [L]

htaccess rewrite different directories

There are 4 rewrite rules, but only the first 2 work:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^news/article/([^/]+)$ /path/to/news/?id=$1
RewriteRule ^news/page/([^/]+)$ /path/to/news/?page=$1
RewriteRule ^company/careers/id/([^/]+)$ /path/to/company/careers.php?id=$1
RewriteRule ^company/careers/page/([^/]+)$ /path/to/company/careers.php?page=$1
/path/to is always the same for all. The two news rules work perfectly, but the two company rules do not.
What's wrong here?
Edit to add (via comments):
This is how the 4 URLs look like.
http://domain.com/folder/folder2/news/article/1
=> translates correctly to http://domain.com/folder/folder2/news/?id=1
http://domain.com/folder/folder2/news/page/2
=> translates correctly to http://domain.com/folder/folder2/news/?page=2
http://domain.com/folder/folder2/company/careers/id/1
=> should translate to http://domain.com/folder/folder2/company/careers.php?id=1, but doesn't
http://domain.com/folder/folder2/company/careers/page/2
=> should translate to http://domain.com/folder/folder2/company/careers.php?page=2, but doesn't
The .htaccess file is located in http://domain.com/folder/folder2/.htaccess.
Change your .htaccess to this:
Options -MultiViews
RewriteEngine On
RewriteBase /folder/folder2/
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^news/article/([^/]+)/?$ news/?id=$1 [L]
RewriteRule ^news/page/([^/]+)/?$ news/?page=$1 [L]
RewriteRule ^company/careers/id/([^/]+)/?$ company/careers.php?id=$1 [L]
RewriteRule ^company/careers/page/([^/]+)/?$ company/careers.php?page=$1 [L]

rewrite rule for WordPress 3.3 permalinks is not working

Every since an upgrade to WordPress 3.3 URLs are not redirecting as they should.
Changed: domain.com/2010/10/postname/ to: domain.com/postname/
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/[0-9]{4}/[0-9]{2}/(.+)$ /$1 [NC,R=301,L]
The problem was due to the leading slash and not using $3
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9]{4})/([0-9]{1,2})/(.+)$ /$3 [NC,R=301,L]
There's a script here you can use to generate .htaccess rules if you want to change permalinks to the /%postname%/ structure.
http://yoast.com/change-wordpress-permalink-structure/
My permalinks were exactly the same as yours, I used this tool to change them and it is working well.
The last rule will never get applied if the previous rule matches. Assuming that the http://domain.com/2010/10/postname/ request doesn't match a file or directory, the RewriteRule . /index.php [L] is going to rewrite the URI to /index.php thus it'll never get to your rule. Try moving your rule up to the top, just below RewriteBase /, and duplicate the !-f/!-d conditions, so that it looks like this:
RewriteBase /
# for 301 redirect
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/[0-9]{4}/[0-9]{2}/(.+)$ /$1 [NC,R=301,L]
# the rest of the rules
RewriteRule ^atom.xml$ feed/ [NC,R=301,L]
RewriteRule ^rss.xml$ feed/ [NC,R=301,L]
RewriteRule ^rss2.xml$ feed/ [NC,R=301,L]
RewriteCond %{HTTP_USER_AGENT} !FeedBurner [NC]
RewriteCond %{HTTP_USER_AGENT} !FeedValidator [NC]
RewriteRule ^feed/?([_0-9a-z-]+)?/?$ http://feeds.feedburner.com/handle [R=302,NC,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
Also, if this is in an .htaccess file, you need to remove the leading slash in the rule match so that it looks like this: ^[0-9]{4}/[0-9]{2}/(.+)$