Conditional Rewrite of URLs that contain an exclamation mark ! - apache

Both
domain.com/blog/post/2011/01/25/This-Is-The-Post-Title!.aspx
domain.com/blog/post/2011/01/25/This-Is-The-Post-Title.aspx
need to be redirected to
domain.com/blog/2011/01/25/this-is-the-post-title
The following rule works for urls without an exclamation mark, but I can't seem to get a condition to strip the ! from the urls if it exists.
RewriteRule ^blog/post(.*).aspx$ /blog${lc:$1} [R=301,L]
Any ideas?

Add this line before your rule:
RewriteRule (.*)!(.*) $1$2 [N,DPI]
This will remove ALL ! characters in URL (path part only, query string is not affected). You can modify it to only apply to blog article titles only -- up to you.
Be careful though -- it uses [N] flag which causes Apache to start rewrite again from topmost rule in order to remove all occurrences, otherwise only first occurrence will be replaced (if there are more than one !). Therefore I recommend putting this rule somewhere on the top.
Alternative for [N] flag would be having this rule without this flag but multiple times one after another.
http://httpd.apache.org/docs/current/rewrite/flags.html#flag_n

I was able to leverage LazyOne's answer and ended up with this:
RewriteEngine On
RewriteRule (.*)!(.*) $1$2 [N,DPI,E=ep:yes]
RewriteCond %{ENV:ep} ^yes$
RewriteRule (.*) https://test.com/$1 [R=301,L]
Works perfectly!

Related

If in htaccess for 301 Redirect special characters anywhere in URL to another characters?

Rule: Redirecting all URLs that contains %252C+ to ,- and redirecting + to - only if the + comes with %252C+.
e.g1: http://www.example.com/FortWorth%252C+TX/
to: http://www.example.com/FortWorth,-TX/
e.g2: http://www.example.com/Fort+Worth%252C+TX/
to: http://www.example.com/Fort-Worth,-TX/
e.g3: http://www.example.com/Fort+Worth/
to: http://www.example.com/Fort+Worth/
(Should not redirect this one)
Note: Please remember your code should not be only for the above URL but for all URLs with the above rule.
Thanks Sumurai8 , for your solution but it needs to be modified like This code :
RewriteEngine On
RewriteRule ^([^+]+)\+(.+)%2C+\+(.*)$ /$1-$2,-$3 [R,L]
RewriteRule ^([^+]+)%2C+\+(.*)$ /$1,-$2 [R,L]
The first argument of RewriteRule is matched against the %-decoded url, so it should be matched against http://www.example.com/FortWorth%2C+TX/ for example instead of http://www.example.com/FortWorth%252C+TX/.
Assuming the strange substring only occurs once, and the "+" only appears once before that, you can do it with the following two rules. If multiple +'s can occur, things start to turn ugly quickly, because you'll need to either add a lot more rules, or start handling it recursively.
RewriteRule ^([^+]+)\+(.+)%2C\+(.*)$ $1-$2,-$3 [R,L]
RewriteRule ^([^+]+)%2C\+(.*)$ $1,-$2 [R,L]
I am not sure if mod_rewrite will escape the , character as a "special character". It is a valid character to use in an url. If it gives you problems, you could use the [NE] flag to prevent escaping such characters.

RewriteRule to remove all slashes

Hi I have tried everything and can't find a solution.
When a user access this URL
http://www.mydomain.com/B/13/B0/BD62CB5382479EFE5EE122BE6FB.jpg?pid=5623
I want it to rewrite it to (not using redirect)
http://www.mydomain.com/B13B0BD62CB5382479EFE5EE122BE6FB.jpg?pid=5623
how do I remove the '/' ?
Your help is very appreciated .
Try with this rule (your htaccess must be located in your document root folder)
RewriteCond %{QUERY_STRING} ^pid=([0-9]+)$
RewriteRule ^/?([^/]+)/([^/]+)/([^/]+)/([^/]+)\.jpg$ /$1$2$3$4.jpg?pid=%1 [L]
EDIT: this code will only work with urls like in your example (with 3 slashes and pid in query string)
EDIT2: to simplify query string if you have multiple params (maybe in different order) we don't need RewriteCond anymore but QSA flag instead
RewriteRule ^/?([^/]+)/([^/]+)/([^/]+)/([^/]+)\.jpg$ /$1$2$3$4.jpg [L,QSA]
If that rule should remove any number of slashes, not just a fixed number, you have to create a loop. TO do so, you can use the \[N\] flag, which will cause the rewriting process to start all over with the current URL. Be careful when doing this, this could lead to infinite loops when done wrong ;)
RewriteRule ^/?(.*)/(.*) $1$2 [N]
Should work however. You can test it with this online tool.
[Edit]: Complete rewrite - I forgot about the [N] Flag ;)

first rewrite rule overriding the rest

quick question for those who know a little more about mod-rewrite than i do:
I have some rules written in sequence like so:
RewriteRule ^(en|fr) index.php?page=home&lang=$1 [L]
RewriteRule ^(en|fr)/home index.php?page=home&lang=$1 [L]
RewriteRule ^(en|fr)/terms index.php?page=terms&lang=$1 [L]
however the first one seems to be over-riding the rest.
I've tried taking off the [L] but this doesnt do what I expected and it will never show the "terms" page.
Any ideas?
If you want http://www.example.com/en or http://www.example.com/fr/ (with or without trailing slash) to redirect to the first item, change the first rule to be:
RewriteRule ^(en|fr)/?$ index.php?page=home&lang=$1 [L]
The /? matches whether or not there's a trailing slash and the $ means "match the end of the URL" (don't match if the URL continues).

Rewrite WordPress permalink URL using mod_rewrite breaks Archive

The following mod_rewrite rule accomplishes the task of rewriting www.domain.com/2011/11/page to www.domain.com/page but breaks www.domain.com/2011/11/ (i.e. breaks WordPress Archive listings) and redirects it to the root of the site.
The rewrite rule should only rewrite items that have content after ^([0-9]{4})/([0-9]{1,2})/page but not ^([0-9]{4})/([0-9]{1,2})/.
RewriteRule ^([0-9]{4})/([0-9]{1,2})/(.*)$ /$3 [NC,R=301,L]
Any recommendations?
ANSWER
The initial forward slash was missing at the beginning:
RewriteRule ^/([0-9]{4})/([0-9]{2})/(.*)$ /$3 [NC,R=301,L]
and the WordPress permalink entry needed:
/%postname%/
instead of:
%postname%
although I am not sure how much the latter helped.
I think you need to change the * to a +
RewriteRule ^([0-9]{4})/([0-9]{1,2})/(.*)$ /$3 [NC,R=301,L]
should be
RewriteRule ^([0-9]{4})/([0-9]{1,2})/(.+)$ /$3 [NC,R=301,L]
With (.), it could possibly match nothing, thus a request like "/2011/11/" will match but the back reference for (.) will be blank, thus the rewrite goes to "/". The + indicates that there needs to be at least 1 character satisfying the "." in the regular expression.

Conditional rewrite in the regex, not with RewriteCond

I wrote a regex for a rewrite rule that includes a negative lookahead to replace the rewriteCond line, because WordPress only accepts two values: pattern -> substitution.
It should find findme.html _here, regardless of where it's requested to be:
mydomain.com/_here/findme.html
e.g.
(Sorry, I can't modify the swf which will request findme.html in the wrong places)
So, given findme.html could be requested to be in, e.g.:
mydomain.com/findme.html
mydomain.com/directory/findme.html
mydomain.com/directory/findme.html?someparam=3
The rewrite should make them all
mydomain.com/_here/findme.html
So, I made a rewrite rule that Wordpress will accept me as follow
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^.*(?!_here)/*findme\.html$ /_here/findme.html [R=301,L]
So it only matches URLs which doesn't contain "_here" in it, to prevent extra rewriting or a loop.
The problem is IT DOES loop.
What did I miss?
It looks to me like you want to move the .* that is before (?!_here) to after it because (?!_here) is a negative lookahead, so it check that the text _here does not come after it. What your regular expression is actually checking is whether your url starts with some character sequence that is not followed by _here, and _here is a character sequence not followed by _here. Then your rule becomes
RewriteRule ^(?!_here).*/*findme\.html$ /_here/findme.html [R=301,L]
Also, it looks like your pattern will exclude paths with subdirectories such as
mydomain.com/directory/subdirectory/findme.html
If you also want to include those, the pattern should be
RewriteRule ^(?!_here)(.*/)*findme\.html$ /_here/findme.html [R=301,L]