RewriteRule to remove all slashes - apache

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 ;)

Related

RewriteRule to remove version number

I have implemented a cache busting solution on a site where a version number is added to the end of the file name, before the file extension. However I am having an issue with one of the rules not working.
Typical requests:
/static/deploy/styles/site_78_direct.min.0.css
/static/deploy/styles/ie/site_78_direct.0.css
I need the above to be redirected to the following:
/static/deploy/styles/site_78_direct.min.css
/static/deploy/styles/ie/site_78_direct.css
I came up with the following which half works:
RewriteRule ^/static/deploy/styles/ie/(.*).([0-9]+).css$ /static/deploy/styles/ie/$1.css [L]
RewriteRule ^/static/deploy/(scripts|styles)/(.*).min.([0-9]+).(js|css)$ /static/deploy/$1/$2.min.$4 [L]
The above redirects properly but I want it to ignore the following:
/static/deploy/styles/ie/site_78_direct-blessed1.css
I basically only want to redirect if there is a number between fullstops, i.e. .0.css
Any help would be greatly appreciated.
Make sure to escape the dot otherwise it will match any character.
Besides just this single rule should work for both cases:
RewriteRule ^(/?static/deploy/.+?)\.\d+\.(js|css)$ $1.$2 [L,NC]

Please help with this mod_rewrite issue

This current system in place needs to allow periods (.) optionally inside of the rewrite condition.
For example: /john.doe should work, since we allow periods in our user names.
However when I add . or \\. or [.] to the following rewrite rule, it either gets stuck in an endless loop, having to restart apache2. Amazingly, the behaviour has changed and I am not sure why. Now it just appends the new string to the existing URL.
For example: /john.doe will become /john.doe/?pg=user&username=john.doe
RewriteRule ^/([a-z0-9_]+)$ /?pg=user&username=$1 [NC,PT]
I am going crazy trying to fix this, please help!
This should work (I'm saying "should" as I do not know how your whole system is set up and what other redirects etc are in place -- I see you are using PT flag (maybe you are using aliases)):
RewriteCond %{REQUEST_URI} !^/index\.php
RewriteRule ^([a-z0-9_\.]+)$ /index.php?pg=user&username=$1 [NC,QSA,L]
OR
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-z0-9_\.]+)$ /index.php?pg=user&username=$1 [NC,QSA,L]
I have specified the script file directly -- it's much more easier to write a rule if you know how it works behind.

Apache Rewrite Directory With Exceptions

I am trying to setup a rewrite that will get any pages that are in the news folder (with the exception of index.shtml and template.shtml (where template.shtml will have a get variable news in it). All other pages should rewrite to template.shtml?news=(same name as news/name).
What I have so far is:
RewriteCond %{REQUEST_URI} !^/news/((index|template).shtml)?$
RewriteRule ^news/(.*) /news/template.shtml?news=$1
This seems to exclude the main /news/, but not template.shtml and the rewrite seems to loop.
How can I resolve this? Any help would be appreciated.
Thanks.
Well -- this one works just fine for me:
RewriteCond %{REQUEST_URI} !^/news/(index|template)\.shtml$
RewriteRule ^news/(.+)$ /news/template.shtml?news=$1 [L,QSA]
This rule will ignore requests to /news/index.shtml and /news/template.shtml.
It will also do nothing when requesting just /news/ (as I have changed .* to .+ to be on a safer side).
Anything else will be rewritten to /news/template.shtml?news=whatever
I've also added the QSA flag to preserve any existing query string (useful for keeping referral data, e.g. /news/hello-pink-kitten?source=google will be rewritten as /news/template.shtml?news=hello-pink-kitten&source=google)

Conditional Rewrite of URLs that contain an exclamation mark !

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!

Apache Mod-Rewrite Question

I have a PHP scripted named index.php inside a folder named blog. There are three different views.
http://www.myDomain.com/blog/index.php
http://www.myDomain.com/blog/index.php?tags=list of categories
http://www.myDomain.com/blog/index.php?post=name of post
I would like to change the view based on the URL.
/blog redirects to number 1 above
/blog/name-of-category redirects to numbe 2 above
/blog/name-of-category/name-of-post redirects to number 3 above.
Right now I have the following mod_rewrite rules.
RewriteRule ^blog$ blog/index.php [L]
RewriteRule ^blog/(.+)/(.+)$ blog/index.php?post=$2 [L]
RewriteRule ^blog/(.+)$ blog/index.php?tags=$1 [L]
This does not work, and I'm not sure why. Right now it always redirects to the last URL:
blog/index.php?tags=$1
And the GET data contains "index.php."
Also, if add a forward slash to the final rule like so:
RewriteRule ^blog/(.+)/$ blog/index.php?tags=$1 [L]
All redirects work fine. The problem is, I'm required to have a forward slash at the end of the URL if I want the category view.
Any ideas what's happening here? how I can fix this?
Thanks for the replies. I figured out that my problem was a side effect of having my scripts inside the folder named "blog". Here's what index.php looked like:
<?php
define ('BASE_PATH', "../blog/");
include_once(BASE_PATH . 'controller/Controller.php');
$controller = new Controller();
$controller->invoke();
See the problem? Because my script's base path was "blog", mod_rewrite was rewriting all my references inside the program. By renaming my script folder to blogScript, it fixed the problem.
In a regular expression, . matches any character (including a / character) so try doing ^blog/([^/]+)$ instead to match any character except a /.
You could write it as follows.
RewriteRule ^blog/?$ blog/index.php [L]
RewriteRule ^blog/(.+?)/(.+?)/?$ blog/index.php?post=$2 [L]
RewriteRule ^blog/(.+?)/?$ blog/index.php?tags=$1 [L]