Removing part of a string in a query string in a URL - apache

I have an input URL that looks something like this:
http://localhost/20north/Numark/product/1/B$#!00$#!4JPPO94$#!
While redirecting this to a new URL, I need to find and remove all occurrences of "$#!" from the last part of the url, so that it becomes:
http://localhost/20north/Numark/product/1/B004JPPO94
Note: The last part can be anything and not just B$#!00$#!4JPPO94$#!. Also, the position of $#! can be anywhere in that last part.

Using mod_rewrite, you just need this rule:
RewriteRule ^(.*)\$#!(.*)$ $1$2 [N]
Edit:
Actually, there seems to be a problem when the $#! is at the end of the URI. Adding an extra rule to remove the trailing match seems to fix it:
RewriteRule ^(.*)\$#!$ $1
RewriteRule ^(.*)\$#!(.*)$ $1$2 [N]
Not quite sure why that was happening.

If you're using php, you could do the following:
<?php
$this_url = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if( strpos($this_url, '$#!') !== false )
die(header('Location: ' . str_replace('$#!', '', $this_url)));
?>
Edit: updated the code to become dynamic

Related

.htaccess rule match part of new url and use as query string for old url

I am trying to match part of a URL and then use the matched expression to append onto the end of a query string from the old URL.
I have the following line in .htaccess, once I've worked out what I'm doing wrong I'll be able to fix the rest so for now I will just focus on the following line:
RewriteRule ^league/([^/]*)$/matches/? index.php?page_id=1074&league=$1
I would like ([^/]*)$ to appear where $1 is
So essentially: /league/29/matches/ would point to index.php?page=1074&league=29
Can anyone please tell me what I am doing wrong? :)
RewriteRule ^league/([^/]*)$/matches/? index.php?page_id=1074&league=$1
The $ (end-of-string anchor) after the subpattern ([^/]*)$, in the middle of the regex, does not make sense here and will cause the regex to fail. You should also be using the + quantifier here (1 or more). You are also missing the end-of-string anchor ($) at the end of the regex (otherwise the trailing /? is superfluous). Although you shouldn't really make the trailing slash optional on the rewrite as it potentially opens you up for duplicate content. (You should redirect to append/remove the trailing slash to canonicalise the URL instead.) You are also missing the L flag on the RewriteRule.
Try the following instead:
RewriteRule ^league/([^/]+)/matches/?$ index.php?page_id=1074&league=$1 [L]
Although, if you are expecting digits only (as in your example) then you should be matching digits, not anything. So, the following is perhaps "more" correct:
RewriteRule ^league/(\d+)/matches/$ index.php?page_id=1074&league=$1 [L]

htaccess Redirect if Contains String WIth Slash

I have tried the following:
RewriteCond %{QUERY_STRING} ^forum/profile$
RewriteRule http://example.com/example.htm? [L,R=301]
If the URL contains "forum/profile" I need it to redirect.
What am I doing wrong? I don't need to escape the forward flash do I?
Why are you using the ^ and $ operators? These operators means that you want match a string that must have the exact forum/profile on the entire string.
eg:
var query_string = forum/profile/something-here
var matches = string.match(^forum/profile$, string) // false because you have string before and after you pattern.
Also, you should check what exactly is given on QUERY_STRING.
Try to add the forward slash before the file name.
Eg;
"/forum/profile.php"
Or use "/?" Before you use "$"
You need to use an absolute path.
Hope this answered your question.

Multiple params HTACCESS

Another question about htaccess
This rule:
RewriteRule ^page(.*)/(.*)$ mainpage.php?var1=$1&var2=$2
simply does not work as it should. When I visit /page/var=something&var2=something I get an empty value for var1 and a value for var2.
RewriteRule ^page(.*)/(.*)$ mainpage.php?var=$1&var=$2
Explanation of what it does:
When you go to /page/var=something&var2=something, apache replaces with you're given RegExp and makes it something like this: mainpage.php?var=&var=var%3Dsomething%26var2%3Dsomething. So, var will be an empty string (length is zero). By the way, you have two var in your query string!
Edit: Note that in example above (/page/var=something&var2=something) $1 would be anything between /page and /var , and since nothing's there, you'll have an empty string!
Edit 2:
RewriteRule /?page/var\=([^&]*)&var2\=([^&]*) mainpage.php?var1=$1&var2=$2

How do I Mod_Rewrite Entire URL Path?

I have been using this rule:
RewriteRule ^([^\.]+)/?$ index.php?page=$1 [L]
Which will turn
example.com/page/hello
...into
example.com/index.php?page/hello
However, if I have a file extension on the end of that, say,
example.com/page/hello/doc.html
...it doesn't work. How can I make that URL become:
example.com/index.php?page/hello/doc.html
...and
example.com/page/
...become:
example.com/index.php?page/
Thank you very much if you are able to help with this. I've tried fiddling with the regular expression, but I just can't make it work.
Thanks.
This section of your regex will match one or more characters that are anything except a full stop:
[^\.]+
If you want to match one or more of any character, then just write:
.+
instead

htaccess question: Remove 1(!) param from URL query

my URL looks like
page.html?L=0&a=1&b=2&c=6 OR
page.html?a=1&b=2&L=0&c=6 OR
page.html?a=1&b=2&c=6&L=0
I need to delete the L=0, where L can be either 0 or 1. But I need to keep all other params. How can this be done?
Try this rule:
RewriteCond %{QUERY_STRING} ^(([^&]*&+)*)L=[01](&+(.*))?$
RewriteRule ^page\.html$ %{REQUEST_URI}?%1%4 [L,R=301]
Ok, I played around a lot on http://gskinner.com/RegExr/.
I simply can't find a regex that works on all these cases:
domain.com/login.html?p1=1&p2=2&L=0&P3=3
domain.com/login.html?p1=1&p2=2&L=1&P3=3
domain.com/login.html?p1=1&p2=2&L=0
domain.com/login.html?&L=0
domain.com/login.html?L=0
domain.com/login.html?L=0&p1=1&p2=2&P3=3
Last best guess was (.)(L=[a-zA-Z0-9]&?)(&?)(.) >>> $1$3$4
This resulted in
domain.com/login.html?p1=1&p2=2&P3=3
domain.com/login.html?p1=1&p2=2&P3=3
domain.com/login.html?p1=1&p2=2&
domain.com/login.html?&
domain.com/login.html?
domain.com/login.html?p1=1&p2=2&P3=3
What I try is:
- Cut out the L param if it is in the middle
- Cut off the L param if it is at the end of a loner param string
- Cut off the L param AND "?" if it is the only param
Can you provide some more hints?
Ok, another headache and I am finally there. I just post this here in case I
missed something
maybe this can help someone else in a similar situation
.htaccess rule
# Rewrite Typo3 links with multilanguage Param "L"
RewriteCond %{QUERY_STRING} (.*)(^L=[a-zA-Z0-9]+&?|^&L=[a-zA-Z0-9]+&|&L=[a-zA-Z0-9]+)(&?.*)
RewriteRule (.*) %{REQUEST_URI}?%1%3 [L,R=301]
Explanation
The rule tries to cover all the following cases
domain.com/login.html?p1=1&p2=2&L=abc&P3=3
domain.com/login.html?p1=1&p2=2&L=def
domain.com/login.html?&L=eh1
domain.com/login.html?&L=asdfasd&p1=1&p2=2
domain.com/login.html?L=0
domain.com/login.html?L=1&p1=1&p2=2&P3=3
As far as my knowledge goes, the regex works as follow:
(.*) checks if there is anything before the following L-param option
The second ( )-pair group contains 3 alternatives/cases on how the L-param could be present in the query string
the third (.*) simply covers everthing that might follow the L-param
Therefor the replacement only needs %{REQUEST_URI}?%1%3. This will cut out the second ( ) group with the different L-param options.
End result
The URL query string will be stripped of the L-param, but leave everything else.
Any comments? Feel free to vote if this is valuable for you, too.
Thanks to all who helped me find the correct solution!