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
Related
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]
I tried going through some of the mod_rewrite questions on the site, but didn't find an answer to my problem (the fact that most questions were titled "mod_rewrite doesn't work" didn't help make that task any easier, hopefully my title is a little more helpful).
this is the content of my /somepath/.htaccess:
RewriteEngine On
RewriteRule ^go/(.*) /cgi-bin/goto.cgi?$1
# ^^^ WORKS: /somepath/go/200001..
RewriteRule ^go?(.*) /cgi-bin/goto.cgi$1
# ^^^ WORKS: /somepath/go?200001..
ErrorDocument 404 /cgi-bin/goto.cgi?error404
In testing, I made goto.cgi simply return $ENV{QUERY_STRING}, $ENV{QUERY_STRING_UNESCAPED} and $ENV{PATH_INFO}. Currently, the above rules mean that I am getting the QUERY_STRING passed on correctly when the urls:
/somepath/go/200001
/somepath/go?200001
/somepath/go?/200001
are accessed, but not when
/somepath/go/?200001
is accessed, in which case $ENV{QUERY_STRING}, $ENV{QUERY_STRING_UNESCAPED} and $ENV{PATH_INFO} are empty.
So the question is, what rule can I use so that
/somepath/go/?200001
gives my script the ?200001 or even /?200001 part back?
The first rule is destroying the query string of the example that fails, since $1 will be empty and an empty query string in the substitution removes any existing query string.
And the second rule is gibberish.
RewriteRule ^go/(.+)$ /cgi-bin/goto.cgi?$1
RewriteRule ^go/?$ /cgi-bin/goto.cgi
I have a website with joomla and I need to redirect (301) some links
They are in this form (index.php?Itemid= identify them - all links that doesn't have this part shouldn't be redirected)
/index.php?Itemid=544&catid=331:savona&id=82356:smembramento-dei-cantieri-baglietto-di-varazze-lopposizione-delle-maestranze&option=com_content&view=article
This should work
RewriteRule ^index.php?Itemid(.*)$ http://www.ligurianotizie.it/archive/index.php?Itemid$1 [L,R=301]
But the first ? (question mark) seems to cause problems.
In fact, if we suppose that the links are without the question mark
/index.phpItemid=544&catid=331:savona&id=82356:smembramento-dei-cantieri-baglietto-di-varazze-lopposizione-delle-maestranze&option=com_content&view=article
I would use
RewriteRule ^index.phpItemid(.*)$ http://www.ligurianotizie.it/archive/index.php?Itemid$1 [L,R=301]
and everything is perfect. But unfortunately real links has that question mark, and I have to find a solution.
What I have to do with that question mark?
Is the ? character escaped? try to add the NE (noescape) flag like this:
RewriteRule ^index.php?Itemid(.*)$ http://www.ligurianotizie.it/archive/index.php?Itemid$1 [L,R=301,NE]
The part behind the question mark is the query string. You can use RewriteCondto determine if it is not empty, and based on that make the decision to redirect.
Note: Query String
The Pattern will not be matched against the query string. Instead, you must use a RewriteCond with the %{QUERY_STRING} variable. You can, however, create URLs in the substitution string, containing a query string part. Simply use a question mark inside the substitution string, to indicate that the following text should be re-injected into the query string. When you want to erase an existing query string, end the substitution string with just a question mark. To combine a new query string with an old one, use the [QSA] flag.
Source: http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html
This should help you:
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} Itemid
RewriteRule ^index.php(.*)$ http://www.ligurianotizie.it/archive/index.php$1 [L,R=301]
Every link containing "Itemid" will be redirected, the others not.
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
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!