.htaccess RewriteRule for change the path if a word appears in the path - mod-rewrite

:-)
I need a .htaccess RewriteRule that can do this:
If the browser load this url path:
http://www.domain.com/example/word_to_change/
change to
http://www.domain.com/example/new_word/
take a look that the "word_to_change" and "new_word" appears in the third level in the path
I was tried this:
RewriteRule ^(word_to_change/)?$ /new_word/ [R,L]
But only works if the "word_to_change" appears in the second level, not in the third.
thanks for your help! :)
ADDED:
three examples:
need to change
http://www.domain.com/second_level_with_any_word/specific_word_1 or
http://www.domain.com/example_1/specific_word_1 or
http://www.domain.com/example_2/specific_word_1
to
http://www.domain.com/second_level_anything/specific_word_2 or
http://www.domain.com/example_1/specific_word_2 or
http://www.domain.com/example_2/specific_word_2
maybe is mor simple for explain with this other example:
http://*/*/specific_word1
for
http://*/*/specific_word2

This depends on what to acomplish, if you want to change ANY word that apeaers after example with a SPECIFIC word then what you need is:
RewriteRule ^example/(.*) /new_path/ [R,L]
if you want to pass that word as a paramater you can use
RewriteRule ^example/(.*) /new_path/process.php?word=$1 [R,L]
if you want to send any URL that ends with a specific word try this
RewriteRule ^(.*)/old_word/$ /$1/new_word/ [R,L]

Related

Regex not matching the whole word in htaccess rule

I'm trying to work out why this doesn't work:
RewriteRule ^search/(.*)-more([0-9]+).html /cgi-bin/search.cgi?bool=and&substring=0&query=$1;nh=$2 [P,L]
RewriteRule ^search/(.*).html /cgi-bin/search.cgi?bool=and&substring=0&query=$1 [P,L]
Example usage:
http://foo.com/search/test.html
http://foo.com/search/test-more-2.html
http://foo.com/search/test%20extra.html
http://foo.com/search/test%20extra-more-2.html
The first 2 work fine - test gets passed along to the query param.
However, on the 2nd one - it gets cutoff at %20 ... so instead of passing test%20extra, all it passes is test
I've compared a similar rule I'm using on another server, and it works fine with (.*) as the selector... so I'm not sure whats different here!
Any suggestions?
Argh... I got it! It wanted NC,L and not P,L as the flags. So:
RewriteRule ^search/(.*)-more([0-9]+).html /cgi-bin/search.cgi?bool=and&substring=0&query=$1;nh=$2 [P,L]
RewriteRule ^search/(.*).html /cgi-bin/search.cgi?bool=and&substring=0&query=$1 [P,L]
Needed to be:
RewriteRule ^search/(.*)-more([0-9]+).html /cgi-bin/search.cgi?bool=and&substring=0&query=$1;nh=$2 [NE,L]
RewriteRule ^search/(.*).html /cgi-bin/search.cgi?bool=and&substring=0&query=$1 [NE,L]

mod_rewrite: transform = and & in slashes

I was just looking for a solution to transform any =,?,& found in a query string into a simple slash /.
To be more specific, my link is something like:
http://www.mydomain.com/product.php?c=1&sc=12&products_id=15
and I would it like this:
http://www.mydomain.com/product.php/c/1/sc/12/products_id/15
whatever the master page could be (in this case is product.php, but it could be foo.php, bar.php...or else).
I have googled a lot but didn't find any good solution to achieve what i'm looking for.
I have found complex rewrite rules, but they all include the "page name" into them:
i.e.
RewriteRule ^/?index/([^/]+)/([^/]+)$ /index.php?foo=$1&bar=$2 [L,QSA]
That rule is only applicable to index.php and to known variables like foo, bar.
I need a more general one, whatever the master page is, whatever the variables are.
Can this be done?
Any suggestion?
Thanks
I assume you're using apache >= 2.2. Add this to your apache conf:
<IfModule mod_rewrite.c>
RewriteEngine On
# you absolutely need to use RewriteBase if this snippet is in .htaccess
# if the .htaccess file is located in a subdirectory, use
# RewriteBase /path/to/subdir
RewriteBase /
RewriteCond %{QUERY_STRING} ^(=|&)*([^=&]+)(=|&)?(.*?)=*$
RewriteRule ^(.*)$ $1/%2?%4= [N,NE]
RewriteCond %{QUERY_STRING} ^=$
RewriteRule ^(.*)$ $1? [R,L]
</IfModule>
The first RewriteCond/RewriteRule pair repeatedly matches a token delimited by & or = and adds it to the path. The important flag is the [N] that causes the whole ruleset to start over again, as often as the rule matches. Additionally, a = is appended to the end of the query string. This is to create a mark in the URL that at least one rewrite has happened.
The second ruleset checks for the = mark that remains after the URL has been rewritten completely and issues a redirect.
Have a look at http://wiki.apache.org/httpd/RewriteQueryString for some useful hints.

Redirect not working for matching a pattern in my url

I am trying to redirect a pattern of urls
http://style.com/style-blog/entry/what-im-looking-for-in-my-next-15-inch-laptop
to
http://style.com/blog/entry/what-im-looking-for-in-my-next-15-inch-laptop
I have tried to match the "style-blog" here
^style-blog/([A-Za-z0-9-]+)$ or ^style-blog/$
I needed to the first version to get ANY remaining part of the url to append to the new url here
RewriteRule ^style-blog/([A-Za-z0-9-]+)$ http://style.com/you-blog/$1 [NC,L]
Thanks for pointers on what I am doing wrong.
I think you may just be missing a /:
RewriteRule ^/style-(blog/[A-Za-z0-9-]+)$ http://style-review.com/$2
Using wildcard:
RewriteRule ^/style-(blog/.*)$ http://style-review.com/$2
Do you even need the full URL?
RewriteRule ^/style-(blog/.*)$ /$2
Are the ^ and $ required? Wouldn't just this work?
RewriteRule style-blog/ blog/
RewriteRule ^style-blog/(.*)/(.*)$ http://%{HTTP_HOST}/you-blog/$1/$2 [R=301,L]
This appears to be working but thanks to you guys you got me on the right track and probably you didn't have enough information to solve it. Its not fully tested yet and might not cope with any extra 'folders' (bits between slashes)

replace character in rewrite rule

Here are my current rules:
RewriteCond %{QUERY_STRING} ^to=(one|seventeen|thirty\+four)
RewriteRule ^/folder/page.php$ http://www.site.com/folder/category/%1? [L]
RewriteRule ^folder/category/(.+)\+(.+)$ http://www.site.com/folder/category/$1-$2 [L]
The first rule works fine, it redirects perfectly if the word is in the query string, but I can't get thirty+four to become thirty-four when redirected.
Any help would be greatly appreciated.
For starters, RewriteRule ^/folder/page.php$ will never match anything. The URI's get the prefix (the leading slash) removed if the rules are in an .htaccess file instead of server config.
Secondly, since you've included http://www.site.com/ in your targets, that means the browser will get redirected instead of internally rewritten. You need to remove http://www.site.com/ from your first rule so that the second one can be applied.
Here's what should work:
RewriteCond %{QUERY_STRING} ^to=(one|seventeen|thirty\+four)
RewriteRule ^folder/page.php$ folder/category/%1 [NC,QSA,L]
RewriteRule ^folder/category/(.+)\+(.+)$ folder/category/$1-$2 [NC,QSA,L]
And now three hints:
1)
Please make sure you've read everything here before asking:
Here's the wiki of serverfault.com
The howto's htaccess official guide
The official mod_rewrite guide
2)
Please try to use the RewriteLog directive: it helps you to track down problems:
# Trace:
# (!) file gets big quickly, remove in prod environments:
RewriteLog "/web/logs/mywebsite.rewrite.log"
RewriteLogLevel 9
RewriteEngine On
3)
My favorite tool to check for regexp:
http://www.quanetic.com/Regex (don't forget to choose ereg(POSIX) instead of preg(PCRE)!)
You use this tool when you want to check the URL and see if they're valid or not.

Re-write userprofile URL (remove.html)

Any Ideas how to change remove .html in this mod-rewrite script
Doesnt work if I remove ".html"
RewriteRule ^([^/]*)\.html$ /userprofile.php?member_id=$1 [L]
Works as
http://site.com/12.html
but wants to have it as
http://site.com/12
Thank you
To make the .html optional, put it in a group and use the ? quantifier:
RewriteRule ^([^/]*)(\.html)?$ /userprofile.php?member_id=$1 [L]
But as this pattern will now also match any single path segment, you should make it more specific to only match your specific URL path pattern. In this case \d+ instead of [^/]* would be a better choice:
RewriteRule ^(\d+)(\.html)?$ /userprofile.php?member_id=$1 [L]
MODIFIED:
I have tested the following and they work on a CentOS server running Apache.
For directory rewriting:
RewriteRule ^/test/(.*)$ http://www.google.com [L,R]
That will redirect http://www.site.com/test to http://www.google.com.
For all files in a directory this will work:
RewriteRule ^/test/([^/]*)(.*)$ http://www.google.com [L,R]
That will redirect www.site.com/test/12.html or www.site.com/test/298.aspx or any other file in the "test" directory to www.google.com.
So this may be more what you are looking for:
RewriteRule ^/12/([^/]*)(.*)$ /userprofile.php?member_id=$1 [L,R]
ORIGINAL POST:
I believe this is what you are looking for:
RewriteRule ^([^/]*)(.*)$ /userprofile.php?member_id=$1 [L]
That is if you are trying to do the rewrite for files...
It will be slightly different if you want to do the rewrite against a directory.
You need to remove the backslash as well ("\.html").

Resources