How to Continue an .htaccess Rule - regex

looking for a little assistance with htaccess rewrite rules:
Original URL
- https://www.example.com/resources-categories/fallbeispiele?lang=de
Desired result:
- https://www.example.com/de/resources-categories/white-papers
Moving around the following htaccess code will process only one rule, I'm researching and finding that [C] will continue, but that's not working.
RewriteCond %{QUERY_STRING} ^[^&](?:\w+)=(\w+)$ [NC]
RewriteRule ^(.*)fallbeispiele(.*)$ $1white-papers$2 [R=301,C]
RewriteRule ^ %1%{REQUEST_URI}? [R=301,L]
Is there an order (in terms of RewriteCond) that I am missing?

This should work:
RewriteCond %{QUERY_STRING} ^(?:\w+)=(\w+)?$ [NC]
RewriteRule ^(.*)fallbeispiele(?:.*)$ %1/$1white-papers? [L,R=301,QSD]

In case anyone is looking for a similar solution:
RewriteCond %{QUERY_STRING} ^(?:\w+)=(\w+)?$ [NC]
RewriteRule ^(.*) %1%{REQUEST_URI}? [L,R=301]
I couldn't use QSD because the Apache version was 2.2
Also, if you're testing with https://htaccess.madewithlove.be - it's not working correctly so don't put all your eggs in one basket, try it out on your own server/staging before disregarding

Related

Handle one query parameter specifically

I need to handle one query parameter specifically.
The url can look like the following:
https://example.com/?app=egov&map=1337
https://example.com/?app=egov&map=1337&showoptions=true
https://example.com/?app=egov&map=1337&showoptions=true&anotherparam=helloWorld
The redirect should point to
https://example.com/contextName/resources/apps/egov/index.html?map=1337
https://example.com/contextName/resources/apps/egov/index.html?map=1337&showoptions=true
https://example.com/contextName/resources/apps/egov/index.html?map=1337&anotherparam=helloWorld
As you can see, the app-Parameter needs to be used as an URL-part; the others need to be used like traditional query-params.
I've figured out, how to implement the first part, where https://example.com/?app=egov points to https://example.com/contextName/resources/apps/egov/index.html using the following
RewriteCond %{REQUEST_URI} ^/$
RewriteCond %{QUERY_STRING} ^app\=(.+)
RewriteRule ^/$ "/contextName/resources/apps/%1/index.html" [R,L,QSA]
but I'm struggeling how to realize the correct handling of the other params.
All these combination of query string can be handled by a single rule loke this:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^app=([\w-]+)(?:&(.*))?$ [NC]
RewriteRule ^$ contextName/resources/apps/%1/index.html?%2 [L]
RewriteCond matches app query parameter and captures it in %1 while rest of the query string after & is captured in %2.
With your shown samples, please try following htaccess Rules file. Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
####Apache documentation link https://httpd.apache.org/docs/trunk/rewrite/remapping.html for more info.
##Internal rewrite rule for URL https://example.com/?app=egov&map=1337
RewriteCond %{QUERY_STRING} ^app=([^&]*)&map=(\d+)$ [NC]
RewriteRule ^/?$ contextName/resources/apps/%1/index.html?map=%2 [L]
##Internal rewrite rule for URL https://example.com/?app=egov&map=1337&showoptions=true
RewriteCond %{QUERY_STRING} ^app=([^&]*)&map=(\d+)&showoptions=(.*)$ [NC]
RewriteRule ^/?$ contextName/resources/apps/%1/index.html?map=%2&showoptions=%3 [L]
##Internal rewrite rule for https://example.com/?app=egov&map=1337&showoptions=true&anotherparam=helloWorl
RewriteCond %{QUERY_STRING} ^app=([^&]*)&map=(\d+)&showoptions=([^&]*)&anotherparam=(.*)$ [NC]
RewriteRule ^/?$ contextName/resources/apps/%1/index.html?map=%2&anotherparam=%4 [L]
All of your URLs uri part is NULL, so rules are written based on that.

Write `.htaccess` regex to redirect URL to previous category/sub-directories

I have a site at www.test.com. The site has many nested directories and categories such as www.test.com/cat/1/321/.
I want to use a Regex to remove any url ending in /321/ or /321 to the previous category.
Example: Redirect www.test.com/cat/1/321/ to www.test.com/cat/1/
Needed for a Wordpress site using .htaccess. Thanks!
You can write your conditions and rules for exam in this code we have to conditions for /321/ and /321
RewriteCond %{REQUEST_URI} /321/$ [NC]
RewriteRule ^(.*)/321/$ /$1 [R=301,L]
RewriteCond %{REQUEST_URI} /321$ [NC]
RewriteRule ^(.*)/321$ /$1 [R=301,L]
Simple enough
RewriteEngine On
RewriteRule ^(.*/)321/?$ $1 [R,L]

Using RewriteRules on query strings from root of domain

I'm trying to rewrite the URLs on my site (to improve SEO, its an AJAX site) so that they go from
http://www.domain.co.uk/?section=home
http://www.domain.co.uk/?section=contact
to
http://www.domain.co.uk/home
http://www.domain.co.uk/contact
respectively.
This is my first time using .htaccess for rewriting, and I'm finding that I end up just guessing with my expressions/copying answers from other questions and trying to make them work.
The closest I've gotten is from adapting this answer here
RewriteEngine on
RewriteCond %{THE_REQUEST} ^GET\ /\?(([^&\s]*&)*)section=([^&\s]+)&?([^\s]*)
RewriteRule ^(.*)$ /section/%3?%1%4 [L,R=301]
RewriteRule ^section/(.*) index.php?section=$1 [L]
which gives me the result
http://www.domain.co.uk/section/contact
however this also breaks all my other links, and leaves the site without CSS or javascript etc.
Furthermore, one of the sections of my site has URLs that look like this
http://www.domain.co.uk/?section=blog&post=post-name-one
http://www.domain.co.uk/?section=blog&post=post-name-two
which I wanted to rewrite to
http://www.domain.co.uk/blog/post-name-one
http://www.domain.co.uk/blog/post-name-two
However, I'm am very unsure about how to approach this in RegEx.
Could anyone help in getting this right? If there are good references for writing these rules, I'd be happy to hear about them as well. So far the things I have read have been helpful in explaining what is happening, but I haven't been able to write my own yet.
You can use these rules in your root .htaccess:
RewriteEngine on
RewriteCond %{THE_REQUEST} \s/+\?section=([^\s&]+)&post=([^\s&]+) [NC]
RewriteRule ^ /%1/%2? [R=302,L]
RewriteCond %{THE_REQUEST} \s/+\?section=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=302,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([^/]+)/([^/]+)/?$ /?section=$1&post=$2 [L,QSA]
RewriteRule ^([^/]+)/?$ /?section=$1 [L,QSA]

Apache Rewrite Issue. Unable to figure out query string

So I have been struggling on finding the rule to match this rewrite. I am working on a client website and it is a nightmare with the number of duplicate title tags. I have managed to resolve most of them by enforcing forward slash, redirect non www. to the www. version and disallow crawling of https version of the website.
The issue I am having at the moment. I have over 1000 URLs that are duplicate content, each product has two different URLs with the exact same content. An example is:
http://www.example.co.uk/product/widget1/
http://www.example.co.uk/widget1/
http://www.example.co.uk/product/widget2/
http://www.example.co.uk/widget2/
Now the following URLs have the same content:
http://www.example.co.uk/product/widget1/
http://www.example.co.uk/widget1/
I want to redirect any URL that contains "/product/" to the URL version without "/product/" in the URL if that makes sense. I honestly don't know where to start and would really appreciate the help.
Thanks in advance
EDIT: The recommended rule:
RewriteEngine On
RewriteRule ^/product/(.*)$ /$1 [R=301]
does not work. It may be conflicting. These are the other rules:
RewriteEngine On
RewriteCond %{QUERY_STRING} .+
RewriteRule ^(.*)$ /$1? [R=301,L]
RewriteRule ^/product/(.*)$ /$1 [R=301]
RewriteCond %{HTTP_HOST} ^example\.co [NC]
RewriteRule (.*) http://www.example.co.uk/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)([^/])$ /$1$2/ [L,R=301]
I dont know if there are any conflicts here. Please help
Have your full .htaccess like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.co [NC]
RewriteRule (.*) http://www.example.co.uk/$1? [L,R=301]
RewriteCond %{QUERY_STRING} .+
RewriteRule ^(.*)$ /$1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+?[^/])$ /$1/ [L,R=301]
RewriteRule ^product/([^/]+)/?$ /$1/ [R=301,L]
Assuming the URLs always start with product, this should work:
RewriteEngine On
RewriteRule ^/product/(.*)$ /$1 [R=301]
It'll need to go in your main site conf or .htaccess

Redirect folder to subdomain and keep subdomain working

Iv'e got such a stupid question here but here we go:
I've got this url: http://www.mysite.com/subsite. I would like to use "/subsite" like this: http://subsite.mysite.com. This is easily achieved with this code:
RewriteRule ^subsite(.*) http://subsite.mysite.com$1 [L]
But here's the stupid part. All my links on my site is already like this: "http://subsite.mysite.com/content". In this case this url should be redirected to "http://www.mysite.com/subsite/content". This creates endless loops (of course).
RewriteRule ^subsite(.*) http://subsite.mysite.com$1 [L]
RewriteCond %{HTTP_HOST} ^subsite\.mysite\.com [NC]
RewriteRule (.*) http://www.mysite.com/subsite/$1 [R]
Is it possible to achieve what i'm aming at?
You need to enable mod_proxy for this.
Then have rules like this:
RewriteCond %{HTTP_HOST} ^(?:www\.)?mysite\.com [NC]
RewriteRule !subsite http://subsite.mysite.com%{REQUEST_URI} [L,R]
RewriteCond %{HTTP_HOST} ^subsite\.mysite\.com [NC]
RewriteRule ^ http://www.mysite.com/subsite%{REQUEST_URI} [L,P]