So I am working with mod rewite
RewriteRule ^/?([a-z]+)$ $1.cfm
so myurl.com/about.cfm is rewritten as myurl.com/about
The problem I am having is with urls with underscores or dashes
myurl.com/another_one.cfm
These do not work I get a 404,
Extends the allowed charset of the pattern.
It is important to put the dash at first position.
Try something like :
RewriteRule ^/?([-_a-z]+)$ $1.cfm
Related
To achieve this URL pattern www.example.com/abc-xyz/mno-pqr/123.html
I am using following in htaccess:
rewriterule ^(.*)/(.*)/(.*).html$ index.php?lyrics_id=$3&singer=$1&song=$2 [L]
Below are example URL which are causing duplicate title error for my site.
www.example.com/abc-xyz/WHATEVER/ANOTHER_WHATEVER/mno-pqr/283.html
www.example.com/abc-xyz/I_DONT_WANT_THIS_PART/mno-pqr/283.html
www.example.com/abc-xyz/HELP_ME/REMOVE/mno-pqr/283.html
www.example.com/abc-xyz/HELP/REMOVE/THIS/PART/mno-pqr/283.html
I want to get only first part before slash in singer part.
I want exactly this,
www.example.com/abc-xyz/mno-pqr/123.html
but not other letters or between abc-xyz and mno-pqr.
Help me writing htaccess.
It seems that you only want to match the url if it consists of exactly 3 parts. You can do this, by matching everything but the delimiter in each part. The delimiter here is /.
Also please note that . matches EVERY CHARACTER. If you want to match the period character instead, you have to escape it (\.).
RewriteRule ^([^/]*)/([^/]*)/([^/]*)\.html$ /index.php?lyrics_id=$3&singer=$1&song=$2 [L]
That works for my 3 parameters rule but how to add 301 redirect to all
those URL which have more than 3 parts?
If the extra parts are always between the first part and the 2nd part of the url, like you showed above, you have to redirect the user with a 301 header:
RewriteRule ^([^/]*)/.*/([^/]*)/([^/]*)\.html$ /$1/$2/$3.html [R=301,L]
I am only just starting to learn how to rewrite urls with the .htaccess file.
How would I change:
http://www.url.net/games/game_one.php
into this:
http://www.url.net/games/game-one/
This is what I have been trying
RewriteRule ^/games/game-one.php ^/games/game-one/ [NC,L]
If you want people to use /games/game-one/ explicitly, you have to rewrite so that it requests /game/game-one.php. So the opposite way around than you have it in your question.
RewriteEngine On
RewriteRule ^games/game-one/$ /games/game-one.php
If you want to rewrite other URL's too, then you'd need to use a technique similar to the prior answer.
Try this:
RewriteRule ^(/games/game-one)\.php $1/
What that says is match anything starting with /games/game-one and remember the first part of that match, then replace it with the first part (capturing group in regex speak), and a slash character. Note that to match a period character you must precede it with a \ since . is a special character that means "any character" (at least if you care to avoid matching any character).
I'm using this tester for url mod_rewrite test:
http://martinmelin.se/rewrite-rule-tester/
When I write something like this:
RewriteRule ^x/([a-z]+)/([0-9]+)/$ x.php?x=$2
Even though I have entered url :
x/p/6/
It get renamed to
x.php?x=p
and. What I want is
x.php?x=6
I'm new with mod rewrite, so anything would be useful.
I'm not a pro in mod_rewrite myself, but I think there is something wrong with that tool.
for example, if you change x.php?x=$2 to x.php?$1=$2, $2 will be parsed correctly.
And then, CMIIW but the regex looks fine. Test the regex here or here.
And Have you ever actually tried the RewriteRule yourself?
RewriteRule ^x/([a-z\/]+?)/([0-9]+)/$ x.php?x=$2
This adds all / characters to the first regex block. The ? makes the search non-greedy.
I want to create my url structure like::
facebook i.e. facebook.com/?pageid=122
For which I am using htaccess mod rewrite as:
RewriteRule ^([a-zA-Z_\-]+)/?([a-zA-Z0-9\-=&_#]*)$ /$1.php?$2 [QSA,L]
so I may translate pages like site.com/home/?pageid=22 into site.com/home.php?pageid=22
The code above works fine except that if I try to add dot like
RewriteRule ^([a-zA-Z_\-]+)/?([a-zA-Z0-9\-=&_#\.]*)$ /$1.php?$2 [QSA,L]
The .htaccess breaks. I need dot so I may pass emails too i.e.
site.com/home/?email=sohaib.dmc#gmail.com
Please help
Try to remove the backslash before the dot. Since it's not considered as a special character inside brackets in a POSIX regular expression.
You need scape the question mark because it's a special character:
RewriteRule ^([a-zA-Z_\-]+)/\?([a-zA-Z0-9\-=&_#\.]*)$ /$1.php?$2 [QSA,L]
The question mark makes the preceding token in the regular expression optional. E.g.: colou?r matches both colour and color.
http://www.regular-expressions.info/optional.html
I wanted to set .htaccess to rewrite:
example.com/bla/info/
to
example.com/info/index.php?q=bla
example.com/bla/info/txt/
to
example.com/info/index.php?q=bla&t=txt
I wanted the browser still to display:
example.com/bla/info/txt/
I didn't want rewrite in the level 2 rewrites, like:
example.com/bla/xxx/
or
example.com/ccc/zzz/aaa/
but
example.com/silly/info/
would work as well as
example.com/strange/info/mytxt/
Did this make sense?
Any help?
If you start your pattern with ^ and end it with $, the rule will only apply if the whole string matches.
RewriteRule ^/bla/info/$ /info/index.php?q=bla
RewriteRule ^/bla/info/txt/$ /info/index.php?q=bla&t=txt
If you use do not use the [R] option, the URL shown in the browser will be whatever the user entered.
Are you trying to make this general-purpose? If so, you can use regex-type matching and variable substitution. To make 'bla' match any string up to the next slash (/), use
([^/]+)
in your pattern and reference it with $1 in your substitution.
RewriteRule ^/([^/]+)/info/$ /info/index.php?q=$1
RewriteRule ^/([^/]+)/info/([^/]+)/$ /info/index.php?q=$1&t=$2
I recommend the Apache web pages about mod_rewrite for more information.
[Edit. Fixed the rules to not include the host name in the pattern, as the original poster figured out.]
--
bmb
you got me in the right track.
i ended up with something like this:
RewriteRule ^([^/\.]+)/info/?$ example/info/index.php?q=$1 [L]
thanks a lot