Apache Mod Rewrite - Replace : character with another - regex

I'm trying to rewrite all URL's that contain a ':' in it to another character.
http://en.wikipedia.org/wiki/Filename#Reserved_characters_and_words
Example:
http://example.com/some_interesting:info
http://example.com/some_interesting_info
http://example.com/some:interesting:info
http://example.com/some:interesting_info
would all point to this file
some_interesting_info
How can I do this?
EDIT: Did more testing
this works
RewriteRule ^(.*)_(.*) $1$2 [L]
RewriteRule ^(.*)\_+(.*) $1$2 [L]
test_rewrite.html goes to testrewrite.html
this doesn't
RewriteRule ^(.*):(.*) $1$2 [L]
RewriteRule ^(.*)\:+(.*) $1$2 [L]
test:rewrite.html gives a 403
In terms of eliminating the character in the middle. Tested with xammp 1.7.1

Try these rules:
RewriteRule ^/([^:]*):([^:]*:.*) /$1_$2 [N]
RewriteRule ^/([^:]*):([^:]*)$ /$1_$2

Here's a link to RewriteRule.
RewriteRule ^/some[_:]interesting[_:]info$ /some_interesting_info [L]

Related

mod_rewrite match QUERY_STRING

So I'm trying to write a rule that will respond with a 404 if certain strings are passed to any of the php scripts. Here's with what I came up with:
RewriteBase /
RewriteCond %{QUERY_STRING} ^.*(string1|string2).*$ [NC]
RewriteRule ^$ [R=404,L]
That rule appears to be matching only www.domain.com/?string1 or www.domain.com/?String2, but not www.domain.com/whatever.php?var=string1 or www.domain.com/directory/script.php?var=string1 or www.domain.com/directory/1/script.php?var=string1 and so on.
Can anyone help and point out what I am doing wrong?
Best,
-Iulian
Your RewriteRule is requiring an empty path. Try it like this:
RewriteBase /
RewriteCond %{QUERY_STRING} ^.*(string1|string2).*$ [NC]
RewriteRule ^.*$ - [NC,L]
As Kevin says, you are requiring an empty URL before the query string, with ^$. You don't need all the .*, you don't have to match the full string. This will work, you don't need the RewriteBase either:
RewriteCond %{QUERY_STRING} (?:string1|string2) [NC]
RewriteRule ^ - [R=404,L]
The ?: just says don't capture this, it's only for grouping. The ^ is a way of matching anything. The - says don't change the URL.

mod_rewrite - redirect to root and replace underscores with dashes

I have been agonising over this for a few hours - trying to get to grips with the syntax for mnod_rewrite, Basically, I am migrating a site so that urls like:
http://www.somedomain.co.uk/news/article/a_blog_post
becomes
http://www.somedomain.co.uk/a-blog-post
The two key things here are removing the "/news/article" bit and replacing underscores with dashes.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/?news/article$
RewriteRule ^([^_]*)_([^_]*_.*)$ $1-$2 [N]
RewriteCond %{REQUEST_URI} ^/news
RewriteRule ^([^_]*)_([^_]*)$ $1-$2 [N]
RewriteRule ^news/article/(.*)$ /$1 [L,R=301]
I can't seem to get the RewriteCond to fire. Please help!
You can do it like this:
RewriteEngine On
# remove /news/article/ when there is no _ left
RewriteRule ^news/article/([^_]+)$ /$1 [L,R=301,NC,NE]
# use recursion based rule to repalce _ by -
RewriteRule ^(news/article/[^_]*)_+(.*)$ $1-$2 [N,DPI]

htaccess rewrite rule: setting first parameter as optional

I'm trying to set the first rule as optional, so both of these URLs will work:
/username
/username/history
/history
The username will never clash.
RewriteRule ^([a-z]+)/((history|analysis|messages|photos)?)$ pages/dashboard.php?username=$1&page=$2 [L]
With the above I have /username/history working. Cannot figure howto get the others.
EDIT: The above snippet is the result of trying to merge these three lines into one.
RewriteRule ^(history|analysis|messages|photos)?$ pages/dashboard.php?page=$1 [L]
RewriteRule ^([a-z]+)/(history|analysis|messages|photos)?$ pages/dashboard.php?username=$1&page=$2 [L]
RewriteRule ^([a-z]+)$ pages/dashboard.php?username=$1 [L]
You can use this rule:
RewriteRule ^(history|analysis|messages|photos)/?$ pages/dashboard.php?page=$1 [L,NC,QSA]
RewriteRule ^([a-z]+)(?:/(history|analysis|messages|photos))?/?$ pages/dashboard.php?username=$1&page=$2 [L,NC,QSA]

Hyphen in regular expression in htaccess

There is this regexp in htaccess:
RewriteRule ^collection/([^/_]+)games/$ some.php?coll=$1 [L]
It works fine for url's like "/collection/somegames/"
But i want the rule to accept these url's: "/collection/some-games/" to be redirected to "some.php?coll=some"
I change it to:
RewriteRule ^collection/([^/_]+)-games/$ some.php?coll=$1 [L]
or
RewriteRule ^collection/([^/_\-]+)-games/$ some.php?coll=$1 [L]
None of the above helped.
What is the solution?
RewriteRule ^collection/([^/_]+?)-?games/$ some.php?coll=$1 [L]
There you go.

Simple regex for .htaccess?

I have a regular expression for a framework to convert host.com/controller/method in to host.com/index.php?controller/method
Like this:
RewriteEngine on
RewriteCond $1 !^(index\.php)
RewriteRule ^(.*)$ /index.php/$1 [L]
I now require it to convert into this,
host.com/controller/method -> host.com/index.php?controller=$1&method=$2
How would I do a match with a regular expression like so within htaccess?
Something like this?
RewriteRule ^(\w+)/(\w+)$ /index.php?controller=$1&method=$2 [L]
If you just want one path segment instead of just anything, use [^/] instead of .:
RewriteCond $1 !=index.php
RewriteRule ^([^/]*)$ /index.php/$1 [L]
And for your second requirement:
RewriteCond $1 !=index.php
RewriteRule ^([^/]*)/([^/]*)$ /index.php?controller=$1&method=$2 [L,QSA]
Here you should also consider to use the quantifier + (one or more) instead of * (zero or more).