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]
Related
I found this rule which works fine with just one condition which is: foobar is in the string.
I need to change this to include a new condition to have two conditions (instead of one):
foobar is in the string. This is already working.
meetball is NOT in the string.
RewriteRule ^(.*)foobar(.*)$ http://www.example.com/index.php [L,R=301]
Please try following, written as per your shown samples. Also you need to create groups (.*) since you are not using them while redirection. You could add NC flag of apache to enable ignorecase to the URI values.
RewriteEngine ON
RewriteRule ^(?!.*metaball).*foobar.*$ http://www.example.com/index.php [NC,L,R=301]
OR without negative lookahead try with usual condition check. Please make sure either you put above Rulesets OR following rulesets one at a time only.
RewriteEngine ON
RewriteRule %{REQUEST_URI} !metaball [NC]
RewriteRule foobar http://www.example.com/index.php [NC,L,R=301]
You can use negative lookahead pattern:
RewriteRule ^(?!.*meetball).*foobar http://www.example.com/index.php [L,R=301]
(?!.*meetball) will fail the pattern match if meatball is found anywhere in URI. Also there is no need to use grouping hence (...) is removed in my answer.
There are a few similar questions on SO, but none that work for this specific scenario.
I want to replace all forward slashes in a URL path with dashes, using mod_rewrite.
So https://stackoverflow.com/foo/bar/baz should redirect to https://stackoverflow.com/foo-bar-baz.
There could be any number of segments in the path (between forward slashes).
I think the solution involves the N flag, but every attempt I've made results in an endless loop.
You can use these 2 rules in your root .htaccess:
RewriteEngine On
RewriteBase /
RewriteRule ^([^/]+)/([^/]+)/?$ $1-$2 [NE,L,R=302]
RewriteRule ^([^/]+)/(.+)$ $1-$2
This will redirect example.com/foo/bar/baz/abc/xyz/123 to example.com/foo-bar-baz-abc-xyz-123
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]
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" :)
I need to have a RegEx that will match a URI like this based on the subdomain "blog"--
http://blog.foo.com/2010/06/25/city-tax-sale/
and redirect like this (getting rid of the subdomain and numbers/date)--
http://foo.com/city-tax-sale/
where the last bit "city-tax-sale" would be a wildcard. So basically any incoming URI that starts with 'blog.foo.com' would be redirected to 'foo.com' + 'whatever is at the end of the above URI after the three sub paths with numbers.
I hope that makes sense. Just trying to create one redirect instead of writing every single one.
This will explicitly match your date format, rather than any series of digits and slashes:
RewriteCond %{HTTP_HOST} ^blog\.foo\.com$ [NC]
RewriteRule ^/\d{4}/\d{2}/\d{2}/(.*)$ http://foo.com/$1 [L,R=301]
The regex part can be broken does to:
^ # start of non-domain url
/\d{4} # slash followed by 4 digits
/\d{2} # slash followed by 2 digits
/\d{2} # slash followed by 2 digits
/ # closing slash
(.*) # rest of the url, captured to group 1
$ # end of url
With the $1 in the replacement being group 1.
In the options part:
L is for "Last" - tells it to not bother looking at other rules.
R=301 is for Redirect with 301 header, which means permanent redirect (just R would send a temporary 302 header)
The RewriteCond bit performs a case-insensitive (NC option) check on the HTTP_HOST header (supplied by user/client) and if it starts blog.foo.com it performs the rewrite, otherwise it doesn't.
RewriteCond %{HTTP_HOST} ^blog.foo.com [NC]
RewriteRule ^(\d+/)+(.*)/?$ http://foo.com/$2 [L,R=301]
You can try this:
/http:\/\/blog\..*\.[a-zA-Z]{2,5}\/[0-9]{4}\/[0-9]{2}\/[0-9]{2}\/(.*)\//