String replace dash to plus sign htaccess - regex

I'm doing a rewrite rewriting my old url structure to my new one, the thing is my old one uses dashes to separate words and my new one uses +'s.
This is my rewrite rule
RewriteRule ^search/files/(.*)/(.*).html?$ http://www.domain.com/search.html?q=$2 [R=301,L]
how could i do a string replace on $2 to replace -'s with +'s?
Thanks

Insert this rule before your existing rule:
RewriteEngine On
# replace - with + in $2 and $2
RewriteRule ^(search/files/[^/]+)/([^-]*)-+(.+?\.html?)$ /$1/$2+$3 [NC,L,R]
# your present rule
RewriteRule ^search/files/([^/]+)/([^.]+).html?$ http://www.domain.com/search.html?q=$2 [R=301,L,QSA,NC]

Related

How to remove the specific character in between using Apache rewrite rule

I have a URL like http://example.com/abc+def+cde+ndk
Unfortunately the number of capturing groups in the URI (abc, def,cde..) are not in a fixed number.
I tried writing a rule like the below but it is matching and replacing only three groups(two character groups and one + in between).
RewriteCond %{REQUEST_URI} ^/(.*?)(\+{1,})(.*)$ [NC]
RewriteRule . http://example.com/%1%3 [R=301,L]
Example given below:
Source: example.com/abc+def+cde+x+y(n number of strings separated by +)
Destination Must be: example.com/abccdexy...till n
If you can add a directive to the main config, the best solution is to use a RewriteMap that processes the URL rewriting through an external script, which you write. You can find details on that here.
Basically you do something like:
RewriteMap convertUrl "prg:/www/bin/convertUrl.pl"
RewriteRule \+ ${convertUrl:%{REQUEST_URI}} [R=301,L]
(only the RewriteMap needs to go in your main config, the RewriteRule can go in your .htaccess)
Where /www/bin/convertUrl.pl is a script you write to process the substitution, as described on the above link. It should take the URL on STDIN (without any buffering), strip out the plus signs, and return it on STDOUT.
Something like this should work:
#!/usr/bin/perl
$| = 1; # Turn off I/O buffering
while (<STDIN>) {
s/\+//g; # Replace dashes with underscores
print $_;
}
Here is a pure .htaccess solution.
# Remove a plus sign on each iteration of the rule
RewriteRule ^help/col/([^+/]+)\+([^/]+)$ help/col/$1$2 [E=REMOVED_PLUS_SIGNS:1]
# For URLs that were processed, redirect once all the plus signs are removed
RewriteCond %{ENV:REMOVED_PLUS_SIGNS} =1
RewriteRule ^help/col/([^+/]+)$ /help/col/$1 [R=301,L]

Rewrite Rule to append

I am currently working on a Rewrite Rule where I need to append certain text into the redirected URL.
The URL that that I want to type into the browser is
http://testwebsite.com/search/?q=SEARCH_STRING
I want this redirected to
http://testwebsite.com/search/SEARCH_STRING/
Basically the SEARCH_STRING needs to be taken from infront of ?= and put after /search/
The current rule that I have is malfunctioning:
RewriteRule ^.*\/search\/\?q=(.*) /#!/search/$1/ [R=301,L,NC,NE]
Any idea how I can fix this ?
You need to capture the query string in a RewriteCond, it's not part of the string your RewriteRule implicitly matches against
RewriteCond %{QUERY_STRING} ^q=(.+)
RewriteRule ^/search/$ /search/%1? [R=301,L,NC,NE]
The trailing ? deletes the existing query string.

rewrite rule issue in htaccess

I have the following rule which is working
RewriteRule ^(.+?)/(step)/([0-9]+)/(id)/([0-9]+)/(start)/([0-9]+)/(end)/
([0-9]+)/?$ index.php?url=$1&$2=$3&$4=$5&$6=$7&$8=$9 [NC,L,QSA]
Now I wanted to add another param at the end of the string which is (ansid) so I did in the following way but for some reason it is not picking up the ansid.
RewriteRule ^(.+?)/(step)/([0-9]+)/(id)/([0-9]+)/(start)/([0-9]+)/(end)
/([0-9]+)/(ansid)/([0-9]+)/?$ index.php?url=$1&$2=$3&$4=$5&$6=$7&$8=$9&$10=$11
[NC,L,QSA]
$10 and $11 won't work because as per Apache mod_rewrite manual:
RewriteRule backreferences:
These are backreferences of the form $N (0 <= N <= 9). $1 to $9 provide access to the grouped parts (in parentheses) of the pattern, from the RewriteRule which is subject to the current set of RewriteCond conditions. $0 provides access to the whole string matched by that pattern.
You need to refactor your rule to use backreference upto $9
Your rule can be possibly rewritten as:
RewriteRule ^(.+?)/(step)/([0-9]+)/(id)/([0-9]+)/(start)/([0-9]+)/end/([0-9]+)/ansid/([0-9]+)/?$ index.php?url=$1&$2=$3&$4=$5&$6=$7&end=$8&$ansid=$9 [NC,L,QSA]

.htaccess replace "/" with "_"

This is my current htaccess
<IfModule mod_rewrite.c>
# turn on rewrite engine
RewriteEngine on
# if request is a directory, make sure it ends with a slash
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*/[^/]+)$ $1/
# if not rewritten before, AND requested file is wikka.php
# turn request into a query for a default (unspecified) page
RewriteCond %{QUERY_STRING} !wakka=
RewriteCond %{REQUEST_FILENAME} wikka.php
RewriteRule ^(.*)$ wikka.php?wakka= [QSA,L]
# if not rewritten before, AND requested file is a page name
# turn request into a query for that page name for wikka.php
RewriteRule ^(.*)$ wikka.php?wakka=$1 [QSA,L]
</IfModule>
My current url structure is
www.domain.com/site/pool/Page_Example_Test
www.domain.com/site/pool/Page_Example_Test/edit
www.domain.com/site/pool/Page_Example_Test/edit?id=1
www.domain.com/site/pool/Page_Example_Test/history
www.domain.com/site/pool/Page_Number_Room
www.domain.com/site/pool/Page_Number_Room/edit
How Is it possible to access them like this
www.domain.com/site/pool/Page/Example/Test
www.domain.com/site/pool/Page/Example/Test/edit
www.domain.com/site/pool/Page/Example/Test/edit?id=1
www.domain.com/site/pool/Page/Example/Test/history
www.domain.com/site/pool/Page/Number/Room
www.domain.com/site/pool/Page/Number/Room/edit
having the htaccess change only those "/" for "_"
There is only /history and /edit finishing the url name nothing more, the normal is without /edit or /history.
If there are between one and five pieces in the page name, such as
Page
Page/Page
Page/Page/Page
Page/Page/Page/Page
Page/Page/Page/Page/Page
and every element starts with a capital letter and every other part of the url is only lowercase, then you can accomplish this with four replacements (the one-piece-only one needs no replacement). For example, for three pieces:
Find what: ([A-Z][a-z]+)\/([A-Z][a-z]+)\/([A-Z][a-z]+)(\/)?
Replace with: $1_$2_$3$4
You can try it here (although I don't understand why it's only replacing with spaces in every line but the last).
Notes:
Each part (...) is captured
The final slash \/? is optional, and is the slash before the potential "edit" or "history".
In some regex flavors, you don't need to escape the slash /, but it's safer to do so: \/.
Each $[number] is a capture group reference
WARNING! These replacements must be done from longest to shortest: five pieces, then four, then three, then two. Otherwise, you'll seriously mess things up.
All the links in this answer come from the Stack Overflow Regular Expressions FAQ. Please consider bookmarking it for future reference. In particular, see the list of online regex testers in the bottom section, so you can try things out yourself.
Place this code in /site/pool/.htaccess:
RewriteEngine On
RewriteBase /site/pool/
RewriteRule "^(Page)/([^/]+)/([^/]+/.*)$" /$1/$2_$3 [L]
RewriteRule "^(Page)/([^/]+)/([^/]+)$" /$1/$2_$3 [L]

mod_rewrite replace '_' with '-'

I'm almost there with a mod_rewrite rule, but I've caved in :)
I need to rewrite
country/[countryname].php
to
country/[countryname]/
however, [countryname] may have an underscore like this: 'south_africa.php' and if it does I want to replace it with a hypen: 'south-africa/'
I also want to match if the country has numbers following it: 'france03.php' to 'france/'
Heres my rule, its almost there but its still adding a hyphen even if there is no second part after the underscore.
RewriteRule ^country/(.*)_(.*?)[0-9]*\.php$ country/$1-$2 [R=301,L]
so currently 'country/south_.php' becomes 'country/south-/'
Can someone please help me find the missing piece of the puzzle? Thanks.
Try this:
RewriteRule ^country/([^_]*)_([^_]*?)\d*\.php$ country/$1-$2 [R=301,L]
This rule will match urls with a single underscore - you'll need a different rule for more underscores or none.
If you want to make sure $2 contains only letter and isn't empty, change ([^_]*?) it to ([a-zA-Z]+).
Alternatively you could do it over several passes:
# If request is for something in "country/"
RewriteCond %{REQUEST_URI} ^country/.+\.php$
# Replace underscore and digits with (single) hyphen
RewriteRule [_0-9]+ \-
# Remove extension (and possible trailing hyphen)
RewriteRule ^(.*)-?\.php$ $1
# Final rewrite
RewriteRule ^country/(.*)$ country/$1 [R=301,L]
Untested ... and not necessarily "pretty" :)