Apache rewrite rule matching one condition but not matching another - regex

I would like to rewrite everything that does not start with wp-content but ends in .html. My failed attempt:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/wp-content.*
RewriteRule ^(.*)\.html /$1-pages/ [R=301,L]

You don't need RewriteCond here. Just modify next rule to:
RewriteRule ^/?(?!wp-content/)(.*)\.html$ /$1-pages/ [R=301,L]
I used a negative lookahead to negate the results.

Related

How to create .htaccess for multiple parameters in URL?

I need using this .htaccess file. It only changes the URL from www.mydomain.com/users/user?usernam=myname123&profile=myprofile123&data=mydata123 to www.example.com/users/myname123/myprofile123/mydata123 .
i use
RewriteEngine On
RewriteRule ^user\.php$ - [L]
RewriteRule ^([0-9a-zA-Z\-_.]*)/?$ /users/user.php?username=$1&profile=$2&data=$3[L,QSA]
htaccess in users folder
not work
With your shown samples, could you please try following rules. This will take anything in REQUEST_URI starting.
RewriteEngine ON
RewriteCond %{QUERY_STRING} ^$
RewriteCond %{THE_REQUEST} /([^/]*)/([^/]*)/([^/]*)/([^/]*)/?\s
RewriteRule ^(.*) /%1?username=%2&profile=%3&data=%4 [NE,L,NC]
OR(either use above or following one) in case you want to match users in URI then run rules then try following.
RewriteEngine ON
RewriteCond %{QUERY_STRING} ^$
RewriteCond %{THE_REQUEST} /users/([^/]*)/([^/]*)/([^/]*)/?\s
RewriteRule ^(.*) /users?username=%1&profile=%2&data=%3 [NE,L,NC]
Explanation: Adding detailed explanation for above.
RewriteEngine ON: Enabling RewriteEngine here to enable mod rule writing.
RewriteCond %{QUERY_STRING} ^$: Checking condition if query string is NULL then go further in rules.
RewriteCond %{THE_REQUEST} /users/([^/]*)/([^/]*)/([^/]*)/?\s: Matching regex in THE_REQUEST from /users to till spaces and capturing 3 groups values into back references to be used later.
RewriteRule ^(.*) /users?username=%1&profile=%2&data=%3 [NE,L,NC]: Using url rewrite to change URI to as per OP's request which has back references values in it.

htaccess RewriteRule regex

I have hundreds of these old links I need to redirect.
Here is one example:
/index.php?option=com_content&view=article&id=433:seventh-character-code-categories-and-icd-10-cm&Itemid=101&showall=1
to
/seventh-character-code-categories-and-icd-10-cm
Essentially I need to remove the /index.php?option=com_content&view=article&id=433: part.
I tried this but I am getting confused with the [0-9] and : parts, so the following does not work:
RewriteRule ^/index.php?option=com_content&view=article&id=[0-9]:(.*)$ /$1 [L,R=301]
Say you want to capture from after : to right before & in the query string you mentioned, then try this expression:
^[^\:]*\:([^\&]*)\&.*$
As #starkeen mentioned in comments, you got to check against the query string. This can be done using RewriteCond %{QUERY_STRING}
So if index.php is in the root folder:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^\/index\.php$
RewriteCond %{QUERY_STRING} ^[^\:]*\:([^\&]*)\&.*$
RewriteRule ^(.*)$ http://example.com/%1 [R=301,L]
Here's another example. This one is for a sub folder:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^\/pages\/index\.php$
RewriteCond %{QUERY_STRING} ^[^\:]*\:([^\&]*)\&.*$
RewriteRule ^(.*)$ /pages/%1? [R=301,L]
Also, notice the ? at the end of the url /pages/%1?, this prevents from re-attaching the query string.
Another thing, captured groups will be set to variables %{number} since set in the RewriteCond.
BTW, depending on your server's configuration, you may need to add the NE flag, like [NE,L,R=301] Plus test whether it is necessary to double escape the literal characters.
what is about direct approach. Skip all till semicolon, mach string till & and replace all with first much
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{QUERY_STRING} [^:]+:([\w-]+[^&]).*
RewriteRule .*$ \/%1? [R=301,L]
</IfModule>

Apache Replace two connected characters to one in url

With apache in the htaccess I would like to replace all periods in the url only when there is an underscore after it.
I want example.com?index.php/blog/low_vs._high to redirect to
example.com?index.php/blog/low_vs_high
Can someone tell me how to do this?
Here is what I have, not sure where to go from here or how to properly debug. Thanks.
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/(.*)\._(.*)$
RewriteRule ^(.*)\.(.*)$ /$1$2 [L,R=301]
You can use this rule:
# execute when there are multiple occurrences of ._
RewriteCond %{REQUEST_URI} ^/(.*)\.(_.*\._.*)$
RewriteRule ^ /%1%2 [L]
# execute when there is only one occurrence of ._
RewriteCond %{REQUEST_URI} ^/(.*)\.(_.*)$
RewriteRule ^ /%1%2 [L,NE,R=301]

mod_rewrite match QUERY_STRING

So I'm trying to write a rule that will respond with a 404 if certain strings are passed to any of the php scripts. Here's with what I came up with:
RewriteBase /
RewriteCond %{QUERY_STRING} ^.*(string1|string2).*$ [NC]
RewriteRule ^$ [R=404,L]
That rule appears to be matching only www.domain.com/?string1 or www.domain.com/?String2, but not www.domain.com/whatever.php?var=string1 or www.domain.com/directory/script.php?var=string1 or www.domain.com/directory/1/script.php?var=string1 and so on.
Can anyone help and point out what I am doing wrong?
Best,
-Iulian
Your RewriteRule is requiring an empty path. Try it like this:
RewriteBase /
RewriteCond %{QUERY_STRING} ^.*(string1|string2).*$ [NC]
RewriteRule ^.*$ - [NC,L]
As Kevin says, you are requiring an empty URL before the query string, with ^$. You don't need all the .*, you don't have to match the full string. This will work, you don't need the RewriteBase either:
RewriteCond %{QUERY_STRING} (?:string1|string2) [NC]
RewriteRule ^ - [R=404,L]
The ?: just says don't capture this, it's only for grouping. The ^ is a way of matching anything. The - says don't change the URL.

RewriteRule double slash issue

Having this regexp in my .htaccess:
RewriteRule ^thumbnails/([0-9]*)/([0-9]*)/(.*)$ lib/thumb.php?w=$1&h=$2&src=$3 [QSA]
I'm having an issue when passing an url in arguments. The regexp remove all slashes but one. Example:
Enter: domain.com/thumbnails/200/143/http://img.youtube.com/vi/xxxxxxx/0.jpg
Result: domain.com/lib.tuhmb.php?w=200&h=143&src=http:/img.youtube.com/vi/xxxxxxx/0.jpg
Note there is just one slash after http:.
Any ideas?
Thanks!
That is an expected behavior in mod_rewrite since rewrite engine strips multiple / into single / while applying pattern in RewriteRule.
To overcome this behavior use RewriteCond %{REQUEST_URI} to capture your values like this:
RewriteCond %{REQUEST_URI} ^/thumbnails/(\d+)/(\d+)/(.*)$ [NC]
RewriteRule ^ lib/thumb.php?w=%1&h=%2&src=%3 [L,QSA]