I'm at my wits end here , I normally like to work things out on my own but this has me well and truly beaten here..
I'm trying to mod rewrite my urls that contain pluses...
/search.php?q=can+be+any+length
to
/can-be-any-length.html
Any help would be really appreciated becaus rewriting the + php is not an option
Using mod_rewrite for this kind of work is not the best option as you can only replace a fixed amount of characters at at time. Using PHP would certainly be easier.
But you can use the "N" (Next) flag to restart replacement, causing the rewrite engine to loop while there's a "+" in the query string:
RewriteCond %{QUERY_STRING} ^(([^&]*&)*)q=([^&+]*)\+([^&+]*\+.*)
RewriteRule ^ %{REQUEST_URI}?%1q=%3-%4 [N]
RewriteCond %{QUERY_STRING} ^(([^&]*&)*)q=([^&+]*)\+([^&+]*)&*(.*)$
RewriteRule ^ /%3-%4.html?%1%5 [L,R=301]
Maybe with RewriteMap :
RewriteMap mymap txt:/path/to/file.txt
RewriteRule ^(.*).html$ search.php?q=${mymap:$1}
Content of file /path/to/file.txt
- +
Related
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]
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.
Apologies if this is a basic solution but mod rewrite still give me gip.
I have a current mod rewrite rule to convert a slug into a usable variable like so:
RewriteRule ^/c/([a-z0-9_\-]+)/?.*$ /content.asp?slug=$1 [L]
I need to add a second, optional variable. Only the slug matters, the second variable may or may not be there.
www.domain.com/this-is-the-slug/
and
www.domain.com/this-is-the-slug/optional-variable/
should both work. I tried:
RewriteRule ^/c/([a-z0-9_\-]+)/([a-z0-9_\-]+)/?.*$ /content.asp?slug=$1&scotland=$2 [L]
But now every page (without the optional variable) just redirects to the root.
any ideas?
I worked it out.
I need both rules:
RewriteRule ^/c/([a-z0-9_\-]+)/([a-z0-9_\-]+)/?.*$ /content.asp?slug=$1&scotland=$2 [L]
RewriteRule ^/c/([a-z0-9_\-]+)/?.*$ /content.asp?slug=$1 [L]
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)
I'm trying to do a very simple rewrite of a query string
http://www.example.com/library.php?q=abscessed-tooth
to
http://www.example.com/library/abscessed-tooth
This is the code that I've written in my .htaccess file and it is doing nothing
RewriteEngine On
RewriteRule ^/library/?([^/]*)/?\/http://www.example.com/library.php?q=$1 [L]
Maybe likely .htaccess files are not considered in your environment. If in doubt turn on RewriteLogging as it is explained in the excellent documentation of the rewriting module.
Oh, and check the error log, you have a syntax error in the RewriteRule anyway: RewriteRule takes 2 arguments plus flags, your rule has only a single argument:
RewriteEngine On
RewriteRule ^library/([^/]*) http://www.example.com/library.php?q=$1 [L]
You need dollar sign in the end of "left" part not question mark:
^/library/([^/]*)/$ http://www.example.com/library.php?q=$1 [L]
Also do you need the question mark between / and ( ? It doesn't look like lookahead or lookbehind?
Try without wrapping slashes as well
^library/([^/]*)$ http://www.example.com/library.php?q=$1 [L]