htaccess rewrite rule not redirecting - regex

I have recently re-coded a website and I have the following line which works great for making a friendly looking URL.
RewriteRule ^mydir/([^/]+)/?$ /page.php?menu=mydir&vid=$1 [L,QSA]
However, I have found out that there are some old links out there which have an altogether different look to them which I also need to rewrite.
So I tried this...
RewriteRule ^mydir/detail/\?id=([^/]+)?$ /page.php?menu=mydir&vid=$1 [L,QSA]
RewriteRule ^mydir/([^/]+)/?$ /page.php?menu=mydir&vid=$1 [L,QSA]
The url containing /detail/ does not rewrite though. Any ideas why?

You cannot match query string using RewriteRule. Change your rules to this:
RewriteCond %{QUERY_STRING} ^id=([^&]+) [NC]
RewriteRule ^mydir/detail/?$ /page.php?menu=mydir&vid=%1 [L,QSA]
RewriteRule ^mydir/([^/]+)/?$ /page.php?menu=mydir&vid=$1 [L,QSA]

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.

How to Continue an .htaccess Rule

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

Regex to Remove Query String From .htaccess

I have a blog that I recently migrated from it's original platform into a self-hosted WordPress site. The old platform appended a query string to URLs for mobile views. This doesn't have any connection in the new responsive site, so URLs with those query strings result in 404 errors.
What I need is a regex for my .htaccess that will strip off the query string ?m=1 from a URL. So, for example, "www.example.com/post/?m=1" should rewrite or redirect to "www.example.com/post/"
What I have so far is this:
RewriteCond %{QUERY_STRING} ^m=1$ [NC]
RewriteRule ^(.*)$ $1? [R=301,L]
Which does absolutely nothing :)
Suggestions?
The problem is the trailing slash in /post/?m=1
#stip trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
RewriteCond %{QUERY_STRING} ^m=1$ [NC]
RewriteRule ^(.*)$ $1? [R=301,L]
My issue was one of syntax. The correct solution is here. Note the importance of placement when using this solution in a WordPress site.

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]

Site structure with multiple query strings (.htaccess)

So basically I want to create a site structure which looks like:
/panel/optional1/optional2/optional3/optional4/
I have tried following:
RewriteRule ^panel/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/?$ index.php?page=panel&e1=$1&e2=$2&e3=$3&e4=$4 [L,QSA]
RewriteRule ^panel/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/?$ index.php?page=panel&e1=$1&e2=$2&e3=$3 [L,QSA]
RewriteRule ^panel/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/?$ index.php?page=panel&e1=$1&e2=$2 [L,QSA]
RewriteRule ^panel/([a-zA-Z0-9]+)/?$ index.php?page=panel&e1=$1 [L,QSA]
But it doesn't work well.
Ok as you asked, try this rule to combine all 4 onto one:
RewriteRule ^panel(?:/([^/]*)(?:/([^/]*))?(?:/([^/]*))?(?:/([^/]*))?)?/?$ /index.php?page=panel&e1=$1&e2=$2&e3=$3&e4=$4 [L,QSA]