Can a RewriteRule process the `-` operator? - .htaccess

I set up the following rule in my htaccess:
RewriteRule ^abc/\w+/xyz/?$ /go.php [L]
I noticed that it would work for:
abc/hello/xyz/
abc/hello_there/xyz/
But will yield 404 if I try:
abc/hello-there/xyz/
Therefore, is there a way to modify the rule so it would read - operator as well? why does it happen?

That's because you didn't include the - in your regex. \w is short for A-Za-z0-9_ so if you want to include a - you need to add it to the expression.
RewriteRule ^abc/[\w-]+/xyz/?$ /go.php [L]

Related

.htaccess: RewriteRule pattern-matching

I'm using mod_rewrite for rewriting my links as follows. I defined a redirect from /test/1234_5678_... to /test.php?id=1234 as follows:
RewriteRule test/(.*)_(.*)$ test.php?id=$1
It works perfectely. Now I wanted to add the following redirect: /test/1234_5678_.../print to /test.php?id=1234&print. Therefore I added the following line before the one above. The redirect is not working and it seems as if only the second rule applies. Am I doing anything wrong with the pattern matching? Is it a problem that there can be more than one underscore and I only used one in the pattern?
RewriteRule test/(.*)_(.*)/print$ test.php?id=$1&print
RewriteRule test/(.*)_(.*)$ test.php?id=$1
Both rules work fine for me, but you probably want to change the first grouping to ([0-9]+) or ([^_]+), and the second group to [^/]+, and add some L flags:
RewriteRule test/([^_]+)_([^/]+)/print$ test.php?id=$1&print [L]
RewriteRule test/([^_]+)_([^/]+)$ test.php?id=$1 [L]

how to attach multiple parameter to index.php using rewrite rule?

hi i am trying to make clean and neat url using rewrite rule
I want to achive :
abc.com/t/param1/param2
Rewite rule that I wrote
RewriteRule ^t/(.+)/(.+)$ t/index.php?v=$1&t=$2 [L]
but it doen't work it redirects to :
http://abc.com/?v=param1&t=param2
Your regex should only match characters that aren't slashes. So your rule should look like
RewriteRule ^t/[^/]+/[^/]+$ t/index.php?v=$1&t=$2 [L]
RewriteRule ^([^/]*)/([^/]*)/$ /?v=$1&t=$2 [L]

.htaccess 301 redirect problem

i try to redirect url's like:
example.com/video/1640/video-name
to
example.com/video/1640/video-name/
i've tried with:
RewriteRule ^video/([^/]*)/([^/]*)/$ video.php?id=$1&title=$2 [L]
RewriteRule ^video/([^/]*)/([^/]*)$ /video/([^/]*)/([^/]*)/ [R=301,L]
but it is not working
my currently htaccess file has only the first line:
RewriteRule ^video/([^/]*)/([^/]*)/$ video.php?id=$1&title=$2 [L]
and videos only loads at
example.com/video/1640/video-name/
url type
i want to redirect the non-backslash url type
example.com/video/1640/video-name
to the correct one (the one with the backslash)
How can i do this?
Your second rule should be RewriteRule ^video/([^/]*)/([^/]*)$ /video/$1/$2/ [R=301,L]
Or you could forgo the redirect totally, and just say RewriteRule ^video/([^/]*)/([^/]*)/?$ video.php?id=$1&title=$2 [L] which will allow both to view your video.
Update FallingBullets is right (see the comments on this answer), his answer better suites the OP's problem, so please ignore this answer (I am leaving it for reference, though).
Maybe you simply have to prefix your pattern with a /?? E. g.
RewriteRule ^/?video/([^/]*)/([^/]*)/$ video.php?id=$1&title=$2 [L]
RewriteRule ^/?video/([^/]*)/([^/]*)$ /video/([^/]*)/([^/]*)/ [R=301,L]
# ^ these ones
instead of
RewriteRule ^video/([^/]*)/([^/]*)/$ video.php?id=$1&title=$2 [L]
RewriteRule ^video/([^/]*)/([^/]*)$ /video/([^/]*)/([^/]*)/ [R=301,L]
since you are anchoring the pattern at the beginning of the path (using ^).

RewriteRule that preserves GET parameters

What is wrong with this rewrite rule?
RewriteRule ^api/(.+)$ api/index.php?url=$1 [L]
I simply want "index.php?url=" to be added after api/ and before the rest of the get parameters.
api/image/upload&arg1=1&text=lorem+ipsum
to
api/index.php?url=image/upload&arg1=1&text=lorem+ipsum
What is wrong with (.+) to get everything after api/?
The regex on the RewriteRule is only run against the path part of the URL, not the query parameters. Fortunately there is the [QSA] flag to preserve existing query parameters.
Are you doing something to stop infinite recursion?
RewriteRule ^api/(.+)$ api/index.php?url=$1 [R=301,L]
or some equivalent
I think you must write your domain name before the regex stuff. Like this:
RewriteRule ^(.+).com/api/(.*)$ "$1.com/api/index.php?url=$2" [R=301,L]

A simple htaccess redirect

How would I write a redirect rule that would let me use both http://www.site.com/rss and http://www.site.com/anydirectory/rss rather than http://www.site.com/rss.xml and http://www.site.com/anydirectory/rss.xml ?
I think I'm close, but for some reason, it's a Monday.
RewriteRule ^(.*)/rss.\xml/$ $1/rss [L]
I think this is what you want (you have them reversed):
RewriteRule ^(.+/)?rss$ $1rss.xml [L]
This is a guess, but it looks like you intended to escape the . and that is done with \. instead of .\
RewriteRule ^(.*)/rss\.xml$ $1/rss [L]
It sounds like you have the XML files, and you want to make URIs without the XML extension work.
# translate /rss to /rss.xml
RewriteRule ^rss$ /rss.xml
# translate /anydirectory/rss to /anydirectory/rss.xml
RewriteRule ^(.+)/rss$ /$1/rss.xml
The code you tried suggests the opposite.
# translate /rss.xml to /rss
RewriteRule ^rss\.xml$ /rss
# translate /anydirectory/rss to /anydirectory/rss.xml
RewriteRule ^(.+)/rss\.xml$ /$1/rss

Resources