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]
Related
I am facing a couple of 301 redirect issues.
Issue 1:
I want to redirect some links to another URL of the same site:
Redirect 301 /path-example.html /dir/subdir/
The redirect is working but it's generating query string like this:
example.com/dir/subdir/?str=path-example
How do I remove the query sting ?str=path-example ?
Issue 2:
I want to redirect some links to the homepage:
Redirect 301 /some-path.html /
But it's leaving a ? after a trailing slash like this:
example.com/?
How do I remove /? from the forwarded URL?
Existing rules:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{QUERY_STRING} ^s=(.*)$
RewriteRule ^$ / [R=301,L]
Thanks!
Please place these rules top of your htaccess files(in case you have more rules in it). Please make sure to clear your browser cache before testing your URLs.
<IfModule mod_rewrite.c>
RewriteEngine ON
RewriteBase /
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteRule ^index\.php/?$ - [L,NC]
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
##Redirection with removing query string rule here.
RewriteCond %{THE_REQUEST} \s/[^.]*\.html\?\S+\s [NC]
RewriteRule ^ /dir/subdir? [R=301,L]
##Redirection to site's home page here for html urls.
RewriteCond %{THE_REQUEST} \s/[^.]*\.html\s [NC]
RewriteRule ^ /? [QSD,R=301,L]
RewriteCond %{QUERY_STRING} ^s [NC]
RewriteRule ^/?$ / [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
EDIT: My first answer without OP's other rules set is as follows:
RewriteEngine ON
##Redirection with removing query string rule here.
RewriteCond %{THE_REQUEST} \s/[^.]*\.html\?\S+\s [NC]
RewriteRule ^ /dir/subdir? [R=301,L]
##Redirection to site's home page here for html urls.
RewriteCond %{THE_REQUEST} \s/[^.]*\.html\s [NC]
RewriteRule ^ /? [QSD,R=301,L]
I need to change url parameters with slashes.
My current working url is
https://www.example.com/Blog-Details.php?blog=test-title
I need to change it like below
https://www.example.com/Blog-Details.php/blog/test-title
My htaccess file is
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#RewriteRule ^([a-z_]+).html $1.php
RewriteRule ^([A-Za-z_-]+).html $1.php
RewriteRule ^([a-z]+)/([a-z]+)/(.*)\.html /market_material.php?ind=$3
#RewriteRule ^([A-Z]{2})/([\+a-z\ ]+)\.html$ /reports_content.php?
report_id=$1
#RewriteRule ^report/([0-9]+) /reports_content.php?report_id=$1
RewriteRule ^([0-9]+)/(.*)\.html /reports_content.php?report_id=$1
#RewriteRule ^blog/([0-9]+) /Blog-Details.php?blog=$1
#RewriteRule ^Blog-Details.html/([^/]+)?$ $1.php?blog=$2 [L]
RewriteRule ^([^.]+?)/?$ /Blog-Details.html?p=$1 [NC,L]
</IfModule>
I added this line to convert parameters with slashes
RewriteRule ^([^.]+?)/?$ /Blog-Details.html?p=$1 [NC,L]
Try using the following RewriteRule in your .htaccess:
RewriteEngine On
RewriteCond %{QUERY_STRING} blog=(.+) [NC]
RewriteRule ^(.*)$ http://www.example.com/Blog-Details.php/%1? [R=301, NC, L]
This is grabbing your variable blog= using a condition and then adding it onto the end of your URL using %1. You'll notice I've also added a ? onto the end of the RewriteRule, this is to stop the original query string from appearing on the end of the URL.
The rewrite works by using 301 redirection, you might want to change this to 302 to make it a temporary redirect while testing.
Make sure you clear your cache before testing this.
I need to rewrite an url from this
https://www.domain.tld/stand-carros-usados/?brand=2&model=&category=&fuel=&kms=&price=
to this
https://www.domain.tld/stand-carros-usados/alfa-romeo
and the code is this
RewriteCond %{REQUEST_URI} ^/stand-carros-usados [NC]
RewriteCond %{QUERY_STRING} ^brand=(.*)&model=(.*)&category=(.*)&fuel=(.*)&kms=(.*)&price=(.*)
RewriteRule ^(.*)$ https://www.domain.tld/stand-carros-usados/alfa-romeo? [L]
The url is changing the one i want but its gives me a 404 page?
Anyone knows why?
EDIT
This is the .htaccess file i have. A bit messi because who created didnĀ“t care that much.
RewriteEngine On
RewriteCond %{HTTPS} off
# First rewrite to HTTPS:
# Don't put www. here. If it is already there it will be included, if not
# the subsequent rule will catch it.
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Now, rewrite any request to the wrong domain to use www.
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteRule ^pt/(.*)$ http://%{HTTP_HOST}/$1 [L,R]RewriteCond %
{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^viaturas(.*)$ https://%{HTTP_HOST}/stand-carros-usados$1 [R=301,L]
RewriteRule ^automoveis(.*)$ https://%{HTTP_HOST}/stand-carros-usados$1 [R=301,L]
RewriteRule ^print/([a-zA-Z0-9_-]+)?$ ajax/printcar.php?id=$1 [L]
RewriteRule ^process-order/([a-zA-Z0-9_-]+)?$ process-order.php?h=$1 [L]
RewriteRule ^([^\.]+)?$ index.php?section=$1 [L]
RewriteRule ^eng/([^\.]+)?$ index.php?section=$2 [L]
RewriteRule ^eng?$ index.php [L]
I have a problem with htaccess on one of these 2 pages: index.php & search.php
Both pages are functional & dependent on variables being sent to them via "GET" method.
SEARCH Example -
search.php?tag=KEYPHRASE
Showing as: site.com/tag/KEYPHRASE
# HTACCESS:
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^tag/(.*)/?$ /search.php?tag=$1 [NC,L]
# internal forward
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^tag/(.+?)/?$ /search.php?tag=$1 [L,QSA,NC]
# external rewrite
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+search\.php\?tag=([^\s]+) [NC]
RewriteRule ^ /tag/%1? [R=302,L]
INDEX Example -
index.php?id=ID&title=TITLE
Showing as: site.com/cover/TITLE-ID
# HTACCESS:
RewriteRule ^cover/(.*)-(.*)/?$ index.php?id=$2&title=$1 [NC,L]
# internal forward FOR SINGLE PAGE (index)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^cover/(.+?)-(.+?)/?$ /index.php?id=$2&title=$1 [L,QSA,NC]
# external rewrite
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?id=([^\s]+)&title=([^\s]+) [NC]
RewriteRule ^ /cover/%2-%1? [R=302,L]
Both pages also have pagination, my problem is, it only works for search.php
SEARCH page w/ pagination:
site.com/tag/KEYPHRASE&page=# WORKS! Correct paginated results being returned.
INDEX page w/ pagination:
site.com/title&page=#-ID Inserts &page=# in between the title & ID in the URL, but shows results.
The only difference I see between these 2 are the number of variables being passed on, otherwise they are being rewritten exactly the same.
I need help with making my .htaccess structure work seamlessly w/ pagination query strings for my index.php page URL.
You need to use QSA flag in your internal rewrite rules and rearrange your rules.
Keep your .htaccess like this:
RewriteEngine On
# external rewrite
RewriteCond %{THE_REQUEST} \s/+index\.php\?id=([^&\s]+)&title=([^&\s]+)&page=([^&\s]+) [NC]
RewriteRule ^ /cover/%2-%1/%3? [R=302,L]
RewriteCond %{THE_REQUEST} \s/+index\.php\?id=([^&\s]+)&title=([^&\s]+)\s [NC]
RewriteRule ^ /cover/%2-%1? [R=302,L]
# external rewrite
RewriteCond %{THE_REQUEST} \s/+search\.php\?tag=([^&\s]+)&page=([^&\s]+) [NC]
RewriteRule ^ /tag/%1/%2? [R=302,L]
RewriteCond %{THE_REQUEST} \s/+search\.php\?tag=([^&\s]+)\s [NC]
RewriteRule ^ /tag/%1? [R=302,L]
# internal forward FOR SINGLE PAGE (index)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^cover/([^-]+)-([^/]+)/([0-9]+)/?$ /index.php?id=$2&title=$1&page=$3 [L,QSA,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^cover/([^-]+)-([^/]+)/?$ /index.php?id=$2&title=$1 [L,QSA,NC]
# internal forward
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^tag/([^/]+)/?$ /search.php?tag=$1 [L,QSA,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^tag/([^/]+)/([0-9]+)/?$ /search.php?tag=$1&page=$2 [L,QSA,NC]
Question:
Is it possible to clear the %{QUERY_STRING} without breaking any functionality of what I've accomplished in my .htaccess file.
I want to prevent users from adding ?foo=bar to the end of the url overwriting any predefined $_GET vars
(external re_write) Example:
From: example.com/foo/bar/hello/world?test=blah
To: example.com/foo/bar/hello/world
So far I have been able to accomplish this (internally):
From: example.com/foo/bar/hello/world
To: example.com/index.php?foo=bar&hello=world
.htaccess
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^GET\s.+\.(?:html?|php|aspx?) [NC]
RewriteRule ^(.+)\.php$ /$1 [R=301,L,NC]
# To externally redirect /dir/index to /dir/
RewriteCond %{THE_REQUEST} ^GET\s((.+)?(\/)?)?(index) [NC]
RewriteRule ^(([^/]+/)*)index$ /$1 [R=301,L,NC]
# To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_URI} !\.php$ [NC]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule . %{REQUEST_URI}.php [L]
# Prevent Rewrite on existing directories, files, and links
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L] #^ - [L]
# To internally rewrite directories as query string
RewriteRule ^([^/=]+)=([^/]*)(/(.+))?\/?$ /$4?$1=$2 [N,QSA]
RewriteRule ^([^/=]+)/([^/=]+)(/(.+))?\/?$ /$4?$1=$2 [N,QSA]
RewriteRule ^([^/=]+)(/(.+))?\/?$ /$3?$1 [N,QSA]
What I've tried:
#if there is a query string
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule (.*) $1? [R=301,L] #remove query string
-
#if there is a query string
RewriteCond %{QUERY_STRING} !=""
RewriteRule (.*) $1? [R=301,L] #remove query string
-
#if there is a query string
RewriteCond %{QUERY_STRING} !=""
RewriteRule ^(.*)$ $1? [R=301,L] #remove query string
I read somewhere, and experienced it, that applying the above breaks anything I've already accomplished. Completely clearing the whole url if they are masked $_GET vars. Even crashes apache 0.o (forgive me for any silly regex or .htaccess code, they're aren't my strong suite and most of it was snippets I found on SO)
is this possible? something I'm doing wrong?
thanks
Try:
RewriteCond %{THE_REQUEST} \?[^\ ]+
RewriteRule (.*) /$1? [R=301,L] #remove query string
You need to match against the actual request because you're building a query string with these rules:
# To internally rewrite directories as query string
RewriteRule ^([^/=]+)=([^/]*)(/(.+))?\/?$ /$4?$1=$2 [N,QSA]
RewriteRule ^([^/=]+)/([^/=]+)(/(.+))?\/?$ /$4?$1=$2 [N,QSA]
RewriteRule ^([^/=]+)(/(.+))?\/?$ /$3?$1 [N,QSA]
When these rules loop around, the %{QUERY_STRING} variable will have stuff in it and your rules will clobber them. If you match against the actual request, your rewrites won't get affected.