# case: leading and trailing parameters
RewriteCond %{QUERY_STRING} ^(.+)?session=[0-9a-z]+&(.+)$ [NC]
RewriteRule (.*) /$1?%1&%2 [R=301,L]
# case: leading-only, trailing-only or no additional parameters
RewriteCond %{QUERY_STRING} ^(.+)?session=[0-9a-z]+$|^osCsid=[0-9a-z]+&?(.*)$ [NC]
RewriteRule (.*) /$1?%1 [R=301,L]
URL is:
https://www.test.com/test/?session=123
Shouldn't this cut off the ?session=123?
If not, how can I achieve this?
You can use this rule to remove a parameter from anywhere in the query string:
RewriteCond %{QUERY_STRING} ^(.*&)?session=[^&]*(?:&(.*))?$ [NC]
RewriteRule ^ %{REQUEST_URI}?%1%2 [L,R=301,NE]
Related
#anubhava and #RavinderSingh13 have continued to help me learn and progress to where I am today with this script. This is what I have so far:
##1st rule with one parameter: $id--
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} id=([0-9]+) [NC]
RewriteRule ^file\.php$ /directory/%1? [R=301,L,NC]
RewriteRule ^directory/(\d+)/?$ /directory/file.php?id=$1 [L,QSA,NC]
##Parameters are $id and $class
RewriteCond %{THE_REQUEST} \s/file\.php\?id=(\d+)&class=(\w+-class)\s [NC]
RewriteRule ^ /directory/%1/%2? [R=301,L]
RewriteRule ^directory/(\d+)/(\w+-class)/?$ /directory/file.php?id=$1&class=$2 [NC,L,QSA]
##Rule with two parameters: $id and $name--
RewriteCond %{THE_REQUEST} \s/file\.php\?id=(\d+)&name=(\S+)\s [NC]
RewriteRule ^ /directory/%1/%2? [R=301,L]
RewriteRule ^directory/(\d+)/(.*)/?$ /directory/file.php?id=$1&name=$2 [NC,L,QSA]```
Now, I would like to add one more rule with only one parameter instead of two. However, this parameter uses PHP $_GET['name'] in the url. I am trying to add one more rule that rewrites http://IPADDRESS.com/file.php?name=JohnDoe to http://IPADDRESS.com/newdirectory/JOHNDOE.
Thank you!
With your shown samples please try following. Please make sure to clear browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} id=([0-9]+) [NC]
RewriteRule ^file\.php$ /directory/%1? [R=301,L,NC]
RewriteRule ^directory/(\d+)/?$ /directory/file.php?id=$1 [L,QSA,NC]
##Parameters are $id and $class
RewriteCond %{THE_REQUEST} \s/file\.php\?id=(\d+)&class=(\w+-class)\s [NC]
RewriteRule ^ /directory/%1/%2? [R=301,L]
RewriteRule ^directory/(\d+)/(\w+-class)/?$ /directory/file.php?id=$1&class=$2 [NC,L,QSA]
##Rule with two parameters: $id and $name--
RewriteCond %{THE_REQUEST} \s/file\.php\?id=(\d+)&name=(\S+)\s [NC]
RewriteRule ^ /directory/%1/%2? [R=301,L]
RewriteRule ^directory/(\d+)/(.*)/?$ /directory/file.php?id=$1&name=$2 [NC,L,QSA]
##Rule with two parameters: $name--
RewriteCond %{THE_REQUEST} \s/file\.php\?name=(\S+)\s [NC]
RewriteRule ^ /directory/%1? [R=301,L]
RewriteRule ^directory/([^/]*)/?$ /directory/file.php?name=$1 [NC,L,QSA]
#anubhava and #RavinderSingh13 have helped me tremendously so far in understanding more about .htaccess and rewrite rules. However, even though both the first rule (with one parameter: $id) and the second rule (with two parameters: $id and $name) works for rewriting the url, my third rule I attempted with also two parameters ($id and $class) fails, and does not rewrite the final url. Below is my entire file so far. I would like to add that the $class parameter is in the format of a-class, b-class, etc., so maybe that could be a contributor to the the rewrite not working? Thank you for helping me continue to learn thus far!
RewriteEngine on
--1st rule with one parameter: $id--
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} id=([0-9]+) [NC]
RewriteRule ^file\.php$ /directory/%1? [R=301,L,NC]
RewriteRule ^directory/(\d+)/?$ /directory/file.php?id=$1 [L,QSA,NC]
--2nd rule with two parameters: $id and $name--
RewriteEngine on
RewriteCond %{THE_REQUEST} \s/file\.php\?id=(\d+)&name=(\S+)\s [NC]
RewriteRule ^ /directory/%1/%2? [R=301,L]
RewriteRule ^directory/(\d+)/(.*)/?$ /directory/file.php?id=$1&name=$2 [NC,L,QSA]
--My attempt at the third rule following the example from the second, but this rule fails. Parameters are $id and $class--
RewriteEngine on
RewriteCond %{THE_REQUEST} \s/file\.php\?id=(\d+)&class=(\S+)\s [NC]
RewriteRule ^ /directory/%1/%2? [R=301,L]
RewriteRule ^directory/(\d+)/(.*)/?$ /directory/file.php?id=$1&class=$2 [NC,L,QSA]
With your shown samples and considering class word will be there always in your 3rd url try following rules. Also you need not to write RewriteEngine ON many times, only 1 time starting of file is enough.
RewriteEngine on
##1st rule with one parameter: $id--
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} id=([0-9]+) [NC]
RewriteRule ^file\.php$ /directory/%1? [R=301,L,NC]
RewriteRule ^directory/(\d+)/?$ /directory/file.php?id=$1 [L,QSA,NC]
##Parameters are $id and $class
RewriteCond %{THE_REQUEST} \s/file\.php\?id=(\d+)&class=(\w+-class)\s [NC]
RewriteRule ^ /directory/%1/%2? [R=301,L]
RewriteRule ^directory/(\d+)/(\w+-class)/?$ /directory/file.php?id=$1&class=$2 [NC,L,QSA]
##Rule with two parameters: $id and $name--
RewriteCond %{THE_REQUEST} \s/file\.php\?id=(\d+)&name=(\S+)\s [NC]
RewriteRule ^ /directory/%1/%2? [R=301,L]
RewriteRule ^directory/(\d+)/(.*)/?$ /directory/file.php?id=$1&name=$2 [NC,L,QSA]
Please help to fix these redirects:
Redirect 1. category/?option=value => domain.com/category/page.html
RewriteCond %{QUERY_STRING} ^option=value(&.*)?$ [NC]
RewriteRule ^category/$ http://domain.com/category/page.html%1 [R=301,NE,NC,L]
Redirect 2. category/sub-%26-category/?option=value => domain.com/category/page1.html
RewriteCond %{QUERY_STRING} ^option=value(&.*)?$ [NC]
RewriteRule ^category/sub-\x26-category/$ http://domain.com/category/page1.html%1 [R=301,NE,NC,L]
You need to use ? in target URI to strip off any existing query string:
RewriteCond %{QUERY_STRING} ^option=value(&.*)?$ [NC]
RewriteRule ^category/$ /category/page.html? [R=301,NE,NC,L]
RewriteCond %{QUERY_STRING} ^option=value(&.*)?$ [NC]
RewriteRule ^category/sub-\x26-category/$ /category/page1.html? [R=301,NE,NC,L]
Make sure these rules are placed above other internal routing rules.
I would like to redirect this url:
http://www.domena.pl/?tekst,123.html
to this
http://www.domena.pl/tekst,123.html
I just want to remove ? after first /, so I would like to redirect every url domena/?...... to domena/......
I was using this htaccess and it works for domena/testtekst,123, but I don't know how I should change it to work with special character ?
RewriteCond %{HTTP_HOST} ^www.domena.pl$
RewriteCond %{REQUEST_URI} ^/test(.*)$
RewriteRule ^test(.*)$ http://www.domena.pl/$1 [L,R=301]
This does not work:
RewriteCond %{HTTP_HOST} ^www.domena.pl$
RewriteCond %{REQUEST_URI} ^/?(.*)$
RewriteRule ^?(.*)$ http://www.domena.pl/$1 [L,R=301]
and this doesn't work too:
RewriteCond %{HTTP_HOST} ^www.domena.pl$
RewriteCond %{REQUEST_URI} ^/\?(.*)$
RewriteRule ^\?(.*)$ http://www.domena.pl/$1 [L,R=301]
You can use:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+\?([^\s&]+) [NC]
RewriteRule ^ /%1? [R=302,L,NE]
Let's say we have an url like this:
http://domain.com/?theme_of_site=parameter_value
http://domain.com/?type_of_site=parameter_value
domain.com - stable/static, always the same
theme_of_site & type_of_site - stable/static, the same when choosen
parameter_value - dynamic
It's like to have always the url's like:
http://domain.com/?theme=parameter_value
http://domain.com/?type=parameter_value
How can I write this in .htaccess?
For the below discussion:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# Start by redirecting the original links from type_of_site= to type=
# Only if the browser's original request contains the strings
RewriteCond %{THE_REQUEST} theme_of_site=([^&]+)
RewriteCond %{THE_REQUEST} type_of_site=([^&]+)
RewriteRule (.*) $1?theme=%1&type=%2 [L,R=301]
RewriteCond %{THE_REQUEST} theme_of_site=([^&]+)
RewriteRule (.*) $1?theme=%1 [L,R=301]
RewriteCond %{THE_REQUEST} type_of_site=([^&]+)
RewriteRule (.*) $1?type=%1 [L,R=301]
# Then, after performing the initial redirects to change the browser
# URL, rewrite the parameters internally
# Match both present:
RewriteCond %{QUERY_STRING} theme=([^&]+)
RewriteCond %{QUERY_STRING} type=([^&]+)
RewriteRule (.*) $1?theme_of_site=%1&type_of_site=%2 [L]
# Then match each individually
RewriteCond %{QUERY_STRING} theme=([^&]+)
RewriteRule (.*) $1?theme_of_site=%1 [L]
RewriteCond %{QUERY_STRING} type=([^&]+)
RewriteRule (.*) $1?type_of_site=%1 [L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Query string parameters need to be captured in RewriteCond. You can match (theme|type) and capture the value, then rewrite it internally using the (theme|type)_of_site in a RewriteRule.
RewriteEngine On
# Match the theme or type paramater in `%1` and capture its value in `%2`
RewriteCond %{QUERY_STRING} (theme|type)=([^&]+)
# Rewrite The parameter name to include _of_site using the values captured above
# for all URLs as captured in $1
RewriteRule (.*) $1?%1_of_site=%2 [L]
The simple example above works only if either of the parameters is specified. If you need to be able to handle both at once like example.com/?type=abc&theme=123 it gets a little more complex:
# Start by redirecting the original links from type_of_site= to type=
# Only if the browser's original request contains the strings
RewriteCond %{THE_REQUEST} theme_of_site=([^&]+)
RewriteCond %{THE_REQUEST} type_of_site=([^&]+)
RewriteRule (.*) $1?theme=%1&type=%2 [L,R=301]
RewriteCond %{THE_REQUEST} theme_of_site=([^&]+)
RewriteRule (.*) $1?theme=%1 [L,R=301]
RewriteCond %{THE_REQUEST} type_of_site=([^&]+)
RewriteRule (.*) $1?type=%1 [L,R=301]
# Then, after performing the initial redirects to change the browser
# URL, rewrite the parameters internally
# Match both present:
RewriteCond %{QUERY_STRING} theme=([^&]+)
RewriteCond %{QUERY_STRING} type=([^&]+)
RewriteRule (.*) $1?theme_of_site=%1&type_of_site=%2 [L]
# Then match each individually
RewriteCond %{QUERY_STRING} theme=([^&]+)
RewriteRule (.*) $1?theme_of_site=%1 [L]
RewriteCond %{QUERY_STRING} type=([^&]+)
RewriteRule (.*) $1?type_of_site=%1 [L]