Create RewriteRule Split query string in .htaccess for _escaped_fragment_ - apache

I am redirecting requests to snapshots directory for google hashbang.
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/$
RewriteCond %{QUERY_STRING} ^_escaped_fragment_=/?(.*)$
RewriteRule ^(.*)$ /snapshots/%1? [NC,L]
So: http://site/?_escaped_fragment_=/detail/10
Go here: site/snapshots/detail/10
But I want to ignore the second params so
http://site/?_escaped_fragment_=/detail/10/12-12-2014
Should be redirect to the same url
site/snapshots/detail/10
Any idea how I should rewrite the rules?

Try changing this line:
RewriteCond %{QUERY_STRING} ^_escaped_fragment_=/?(.*)$
to
RewriteCond %{QUERY_STRING} ^_escaped_fragment_=/?([^/]+(?:/[^/]+|))
So the regex will only attempt to capture as many as 2 virtual paths.

Related

RedirectMatch 301 remove query string / URL Parameters

i need a Solution for the google search console error links and RedirectMatch 301 remove query string / URL Parameters in my htaccess:
Google Search Console Links:
https://www.domain.de/?main_page=index&zenid=umf5etlrmr4blnuiq0e4jsp6l2&cPath=15_326&sort=20a&alpha_filter_id=68
https://www.domain.de/index.php?main_page=product_reviews_write&products_id=9985&cPath=5_380&number_of_uploads=0
https://www.domain.de/?main_page=index&cPath=46_47&sort=20a&alpha_filter_id=70
https://www.domain.de/?currency=USD&main_page=products_new&disp_order=7&page=141
https://www.domain.de/?main_page=index&zenid=mj6nsb9r53goiu6e13nb80tfq7&cPath=1_160&sort=20a&alpha_filter_id=70
https://www.domain.de/?currency=USD&main_page=index&cPath=3_137
https://www.domain.de/?main_page=index&cPath=46_76&sort=20a&alpha_filter_id=84
https://www.domain.de/?main_page=index&cPath=5_6&sort=20a&alpha_filter_id=85
https://www.domain.de/index.php?main_page=index
And many more.
I test this way in the htaccess, but it didn't work:
RedirectMatch 301 ^/?main_page*$ https://www.domain.de/
RedirectMatch 301 ^/?main_page\=$ https://www.domain.de/
RewriteEngine on
RewriteCond %{QUERY_STRING} ^main_page$ [NC]
RewriteRule ^/?main_page?$ https://www.domain.de/ [L,R=301]
You can use the following generic rule to remove query strings
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(main_page|currency) [NC]
RewriteCond %{REQUEST_URI} ^/$ [OR]
RewriteCond %{REQUEST_URI} ^/index\.php$ [NC]
RewriteRule ^ %{REQUEST_URI}? [L,R=301]
Adding one more approach of htaccess rules here, written as per shown samples. Using Apache's THE_REQUEST variable here.
Make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{THE_REQUEST} \s/(?:index\.php)?/?(?:\?(?:main_page|currency))?\s [NC]
RewriteRule ^ %{REQUEST_URI}? [L,R=301]
OR as an alternative use following code(using QSD flag here to remove query string):
RewriteEngine ON
RewriteCond %{THE_REQUEST} \s/(?:index\.php)?/?(?:\?(?:main_page|currency))?\s [NC]
RewriteRule ^ %{REQUEST_URI} [L,R=301,QSD]

Apache redirect & rewrite

This is my page
http://example.com/index.html .
I would like to make it work like this.
I would like to take the user to http://def.com when he requests
http://example.com/index.html
Even if he requests http://example.com/index.html?headers=0, he should be taken
to http://def.com
But if he requests the same URL with other query parameters, say for example http://example.com/index.html?footer=1, then the redirect or rewrite shouldn't happen. He should still be seeing the response from http://example.com/index.html?footer=1
I have already tried
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^/index.html$ http://def.com
Change your htaccess file to this:
RewriteCond %{QUERY_STRING} ^(headers=0|)$ [NC]
RewriteRule ^index.html$ http://def.com [R=301,L]
Change your htaccess file to this:
RewriteCond %{QUERY_STRING} ^$ [OR]
RewriteCond %{QUERY_STRING} !headers
RewriteRule ^index.html$ http://def.com [R=301,L]

How to redirect a URL with a querystring

I've moved a website to a new domain and I've used RewriteRule to redirect a number of my URLs.
I'm stuck trying to redirect URLs with query strings in them?
RewriteRule /news/archive.php?cat=economic-impact http://www.new-website.com/faqs/economic-impact [R=301,L]
RewriteRule /news/archive.php?cat=housing-need http://www.new-website.com/faqs/housing-need R=301,L]
RewriteRule /news/archive.php?cat=infrastructure http://www.new-website.com/faqs/infrastructure R=301,L]
Further up the htaccess file I'm using this to redirect the domain
RewriteEngine On
RewriteCond %{HTTP_HOST} ^old-website.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.old-website.com$
RewriteRule (.*)$ http://www.new-website.com/$1 [R=301]
When I visit:
old-website.com/news/archive.php?cat=economic-impact
it redirects to
new-website.com/news/archive.php?cat=economic-impact
Which 404s, I need to to redirect to:
new-website.com/faqs/economic-impact
Can anyone help?
You need to add a blank query string on your rewrite to truncate any existing query strings.
The below should do the job:
RewriteRule (.*)$ http://www.new-website.com/$1? [R=301]
In addition, the way you're checking the existing query strings won't work. You'll need to check them separately:
RewriteCond %{QUERY_STRING} "cat=(economic-impact|housing-need|infrasructure)"
RewriteRule news/archive.php http://www.new-website.com/faqs/%1? R=301,L]
The %1 will take a backreference from a regex group match in the RewriteCond

Apache redirect using RewriteEngine with and without query string

I'm trying to get 2 redirections set on my htaccess file but I can't make it to work.
The desired output is:
if the user goes to www.mywebsite.com/page1, it gets redirected to /folder/newPage
if the user goes to www.mywebsite.com/page1?a=123, it gets redirected to /folder/anotherPage?a=123
I tried this
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^/page1$ /folder/newPage [R=301,L]
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^/page1(.*)$ /folder/anotherPage$1 [R=301,L]
I also tried something like this:
RewriteCond %{REQUEST_URI} ^/page1$
RewriteRule ^(.*)$ /folder/newPage [R=301,L]
RewriteCond %{REQUEST_URI} ^/page1$
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*)$ /folder/anotherPage%1 [R=301,L]
Note that Rewrite mod is turned on on my apache.
Any idea how to make it work?
Check through the following:
RewriteEngine On is present before all the rules
AllowOverride directive allows your htaccess files to load
htaccess receives URLs without the leading slashes, unlike server config files, or VHosts file.
.* matches 0 or more characters, thus %{QUERY_STRING} ^(.*)$ also matches empty query strings. Change this to .+.
Final rules should be:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^page1$ /folder/newPage [R=301,L]
RewriteCond %{QUERY_STRING} ^(.+)$
RewriteRule ^page1$ /folder/anotherPage?%1 [R=301,L]

.htaccess rewrite subdomain to directory

Is it possible to use .htaccess to rewrite a sub domain to a directory?
Example:
http://sub.domain.example/
shows the content of
http://domain.example/subdomains/sub/
Try putting this in your .htaccess file:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^sub.domain.example
RewriteRule ^(.*)$ /subdomains/sub/$1 [L,NC,QSA]
For a more general rule (that works with any subdomain, not just sub) replace the last two lines with this:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.example
RewriteRule ^(.*)$ subdomains/%1/$1 [L,NC,QSA]
I'm not a mod_rewrite expert and often struggle with it, but I have done this on one of my sites. It might need other flags, etc., depending on your circumstances. I'm using this:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com$
RewriteCond %{REQUEST_URI} !^/subdomains/subdomain
RewriteRule ^(.*)$ /subdomains/subdomain/$1 [L]
Any other rewrite rules for the rest of the site must go afterwards to prevent them from interfering with your subdomain rewrites.
You can use the following rule in .htaccess to rewrite a subdomain to a subfolder:
RewriteEngine On
# If the host is "sub.domain.example"
RewriteCond %{HTTP_HOST} ^sub.domain.example$ [NC]
# Then rewrite any request to /folder
RewriteRule ^((?!folder).*)$ /folder/$1 [NC,L]
Line-by-line explanation:
RewriteEngine on
The line above tells the server to turn on the engine for rewriting URLs.
RewriteCond %{HTTP_HOST} ^sub.domain.example$ [NC]
This line is a condition for the RewriteRule where we match against the HTTP host using a regex pattern. The condition says that if the host is sub.domain.example then execute the rule.
RewriteRule ^((?!folder).*)$ /folder/$1 [NC,L]
The rule matches http://sub.domain.example/foo and internally redirects it to http://sub.domain.example/folder/foo.
Replace sub.domain.example with your subdomain and folder with name of the folder you want to point your subdomain to.
I had the same problem, and found a detailed explanation in http://www.webmasterworld.com/apache/3163397.htm
My solution (the subdomains contents should be in a folder called sd_subdomain:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} subdomain\.domain\.example
RewriteCond $1 !^sd_
RewriteRule (.*) /sd_subdomain/$1 [L]
This redirects to the same folder to a subdomain:
.httaccess
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^([^\.]+)\.domain\.example$ [NC]
RewriteRule ^(.*)$ http://domain\.example/subdomains/%1
Try to putting this .htaccess file in the subdomain folder:
RewriteEngine On
RewriteRule ^(.*)?$ ./subdomains/sub/$1
It redirects to http://example.org/subdomains/sub/, when you only want it to show http://sub.example.org/.
Redirect subdomain directory:
RewriteCond %{HTTP_HOST} ^([^.]+)\.(archive\.example\.com)$ [NC]
RewriteRule ^ http://%2/%1%{REQUEST_URI} [L,R=301]
For any sub domain request, use this:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.band\.s\.example
RewriteCond %{HTTP_HOST} ^(.*)\.band\.s\.example
RewriteCond %{REQUEST_URI} !^/([a-zA-Z0-9-z\-]+)
RewriteRule ^(.*)$ /%1/$1 [L]
Just make some folder same as sub domain name you need.
Folder must be exist like this: domain.example/sub for sub.domain.example.
Edit file .htaccess:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]