htaccess - multiple rewrites & capture groups - regex

I'm trying to handle multiple areas of an application, but the URLs are not being rewrited as expected.
This is the .htaccess file:
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^division/(.*)/section/(.*)$ ./index.php?division=$1&section=$2
RewriteRule ^division/(.*)$ ./index.php?division=$1
RewriteRule ^area/(.*)$ ./index.php?area=$1
What's expected:
If the URI matches division/some_division/section/some_section rewrite to index.php?division=some_division&section=some_section
If no section (section/some_section) is defined, go to the second rule -
If the URI matches division/some_division rewrite only to index.php?division=some_division
If no division is defined, and the URI matches area/some_area rewrite to index.php?area=some_area
I'm almost sure I can combine the two first rules, I've tried this regex but it didn't work:
^division/(.*)( /section/(.*) )?$
It supposed to make /section/some_section an optional value.
Unfortunately nothing works. Any ideas?

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^division/(.*)/section/(.*)$ ./index.php?division=$1&section=$2 [QSA,L]
RewriteRule ^division/(.*)$ ./index.php?division=$1 [QSA,L]
RewriteRule ^area/(.*)$ ./index.php?area=$1 [QSA,L]

Related

RewriteRule if first character is question mark

I have a problem with my .htaccess file.
At the moment I use the following script for every request under the subdir /lp:
RewriteEngine On
RewriteBase /lp
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^.*\.(jpg|css|js|gif|png)$ [NC]
RewriteRule . index.php
If someone uses an URL like the following
www.website.com/?1FOOBAR23
I would also like to redirect to the subdirectory /lp with the same rule as above. But only if the URL starts with a question mark followed by a number and 6-8 other chars. How could I do that?
You will need this rule in site root .htaccess:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^[a-z0-9]{6,}$ [NC]
RewriteRule ^/?$ ip/index.php [L]

Allowing parameters with DOT in .htaccess

I have this on my .htaccess
RewriteEngine On
RewriteRule ^([^/\.]+)/?$ ?link=$1 [L]
My link is example.com/?link=sometext
I want to acces that by example.com/sometext and this^ rewrite does that
Now my links has changed, they are now in this format-> 'some.text' . With a dot. The current htaccess gives me 404
How can I make this work ? I tried generators but they only gave me 500 errors
Remove . from your character class and have your rule as this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ ?link=$1 [L,QSA]

URL rewriting regex with optional capturing

I have these types of URLs:
/manage/123
/manage_foo/456
/manage_bar/789
/manage
/manage_foo
/manage_bar
I need to rewrite to:
/manage.php?id=123
/manage_foo.php?id=456
/manage_bar.php?id=789
/manage.php?id=
/manage_foo.php?id=
/manage_bar.php?id=
How do I make the capturing of the id optional? I know this works if an id is present:
RewriteCond %{REQUEST_URI} ^\/(manage.*)\/(.*)$
RewriteRule ^ %1.php?id=%2 [NC,L]
That regex breaks down if you remove the slash before the id
You can use this rule:
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(manage)(?:/([0-9]*))?/?$ $1.php?id=$2 [QSA,L,NC]

.htaccess mod_rewrite rules conflicting

I have the following rewrite rules:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !\.(css|gif|ico|jpg|js|png|swf|txt)$
RewriteCond %{REQUEST_URI} !^/(Account|Logout|Password|Tags) [NC]
RewriteRule ^([^/]*)$ /index.php?city=$1 [QSA,L]
RewriteCond %{REQUEST_URI} ^/Account [NC]
RewriteRule ^Account /members/account.php [NC,L]
RewriteCond %{REQUEST_URI} ^/Logout [NC]
RewriteRule ^Logout /members/logout.php [NC,L]
RewriteCond %{REQUEST_URI} ^/Password [NC]
RewriteRule ^Password /members/password.php [NC,L]
RewriteCond %{REQUEST_URI} ^/Tags [NC]
RewriteRule ^Tags /members/tags.php [NC,L]
I'm trying to add another condition that so that it loads as
domain.com/$city/$provider/$name
this is the rule:
RewriteRule ^([^/]*)/([^/]*)/([^/]*)$ /listing.php?city=$1&provider=$2&urlname=$3 [L]
My issue is that this rule conflicts with the rule for my index file which loads as domain.com/$city.
I'd greatly appreciate any and all suggestions.
Thanks!
There's nothing wrong with any of your rules. Its just that the first covers the last in terms of what it will accept. The easiest way to avoid this problem is to move the more restrictive rule above of the more general. I.e. in this case put the Listing rule immediately before the index.php rule.
Addendum
Oops, The ->listing.php followed by the ->indexp.php rules will still loop because rule1 fires on the first pass and then rule2 on the second, since the query string is stripped for purposes of regexp matching in a rule; and "listing.php matches ^([^/]*)$.
You should only match ^([^/]*)$ in the case where the pattern doesn't match an existing file. The way to prevent this is to put a
RewriteCond %{REQUEST_FILENAME} !-f
before the index.php rule.

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}/(.+)$