mod rewrite: Rewrite rule mach slashes and change them to get variables - regex

I'm using index.php as my entry script. I want to apply specific Rewrite rules to my htaccess to see my URLs in better format
For now I have succeeded to match my controller & action only by using
RewriteRule ^(.*)$ /mysite/index.php?r=$1 [L]
So I can use any of these URLs
domain.com/users/user => /mysite/index.php?r=users/user
domain.com/contacts/contact = /mysite/index.php?r=contacts/contact
I want to add some additonal get variables to my URLs, to get specific records like
domain.com/users/user/id/10
or
domain.com/contacts/contact/id/2/name/5
Now I can do this only this way:
domain.com/users/user&id=10 = > /mysite/index.php?r=users/user&id=10
domain.com/contacts/contact&id=2&name=5 => /mysite/index.php?r=contacts/contact&id=2&name=5
How can I change my Rewrite rule to support GET variables as well?

Add QSA flag:
RewriteRule ^(.*)$ /mysite/index.php?r=$1 [L,QSA]
QSA stands for Query String Append which make sure to preserve existing query string while adding new query parameters.
Reference: Apache mod_rewrite Introduction

Related

Using htaccess to redirect anything after a parameter - regex

Id like to have the following URL(s) redirect to the same URL just without the ?
For example:
https://www.example.com/this-is-static?numbersletterssymbols
goes to
https://www.example.com/this-is-static
"numbersletterssymbols" can be anything
Id like this to be a 301 , using htaccess ( apache )
I came across the following, however, the variable seems to be in parentheses
RewriteCond %{QUERY_STRING} ^product=(.*)$
RewriteRule ^test.php$ %1/? [R=301,L]
Any insight is appreciated
To remove the query string (any query string) from any URL you could do the following using mod_rewrite, near the top of your .htaccess file:
RewriteEngine On
RewriteCond %{QUERY_STRING} .
RewriteRule ^ %{REQUEST_URI} [QSD,R=301,L]
The condition (RewriteCond directive) simply asserts that there is a query string consisting of at least 1 character (determined by the regex . - a single dot).
The QSD (Query String Discard) flag removes the original query string from the redirected response. The QSD flag requires Apache 2.4 (which you are most probably using). The method used on earlier versions of Apache, as in your example, is to append a ? to the susbstitution string (essentially an empty query string).
Note that you should test first with a 302 (temporary) redirect to avoid potential caching issues.
however, the variable seems to be in parentheses
The parentheses in the regex simply creates a "capturing group" which can be referenced later with a backreference. eg. In your example, the value of the product URL parameter is referenced in the RewriteRule substitution string using the %1 backreference in order to redirect to the value of the URL parameter. This is very different to what you are trying to do and is arguably a security issue. eg. It would redirect a request for /test.php?product=https://malicious.com to https://malicious.com/, allowing a potential hacker to relay traffic via your site.
UPDATE: is it possible to make this work only for when the URL begins with "this-is-static" (for example)
Yes, the RewriteRule pattern (1st argument) matches the URL-path, less the slash prefix. For example:
RewriteCond %{QUERY_STRING} .
RewriteRule ^this-is-static %{REQUEST_URI} [QSD,R=301,L]
Matches all URLs that start with /this-is-static.

.htaccess URL rewrite issue with query parameters

I have a .htaccess rule that goes like this
RewriteRule ^account/(.*) myaccount.php?p=$1 [NC,L]
This works fine for URLs like https://www.example.com/account/abcd.
However, now I want to pass query parameters to this URL, something like https://www.example.com/account/abcd?ab=1&cd=2.
Can't figure out the exact .htaccess rule that can accommodate this.
Any pointers?
Use:
RewriteRule ^account/(.*) myaccount.php?p=$1 [QSA,NC,L]
With QSA:
QSA|qsappend
When the replacement URI contains a query string, the
default behavior of RewriteRule is to discard the existing query
string, and replace it with the newly generated one. Using the [QSA]
flag causes the query strings to be combined.
http://httpd.apache.org/docs/current/rewrite/flags.html

Obtain url get variables when htaccess is already creating a query string

I have a rule in .htaccess that generates a query string according to the url syntax:
RewriteEngine on
RewriteRule a_([0-9]+)_(.*).html$ article.php?id=$1 [L]
So a url like this:
a_52_how-to-use-htaccess.html generates: article.php?id=52
My question is how to allow htaccess to add to the query string other get variables sent on the url:
a_52_how-to-use-htaccess.html?debug=true
in order to have:
article.php?id=52&debug=true
Any ideas? Thanks.
Use QSA flag:
RewriteEngine on
RewriteRule a_([0-9]+)_(.*)\.html$ article.php?id=$1 [L,QSA]
QSA (Query String Append) flag preserves existing query parameters while adding a new one.

How to add a catch all to an htaccess redirect regular expression?

I have the following line in my htaccess
RewriteRule ^([^/.]+)/([^/.]+)/r/([^/.]+)/?$ /index.php?slug_1=$1&slug_2=$2&ref=$3 [L]
So, blah.com/a/b/r/c/ goes to blah.com?slug_1=a&slug_2=b&ref=c
I would also like to catch and append all extra query params
So if I go to blah.com/a/b/r/c?d=e&f=g then it redirects to blah.com?slug_1=a&slug_2=b&ref=c&d=e&f=g
Your rule is correct but you need QSA flag:
RewriteRule ^([^/.]+)/([^/.]+)/r/([^/.]+)/?$ /index.php?slug_1=$1&slug_2=$2&ref=$3 [L,QSA]
QSA is called Query String Append that preserves original query string while adding new one.

Apache rewrite remove .jpg.html from URL

I've been trying to write a rewrite rule for apache to switch my gallery2 URLs to the gallery3 URL format:
Old url example linked: http://domain.com/gallery/photoalbumxyz/photo.jpg.html
New url example needed: http://domain.com/photos/photoalbumxyz/photo
Note that in the URL example above, "/photoalbumxyz/photo.jpg.html" is not an actual physical directory, it is just the way gallery2 rewrote "friendly" URLs. I can rewrite the /gallery/ to /photos/ by using a rule like the following:
RewriteRule ^(.*)$ /photos/$1 [QSA,L,R=301]
However I'm having trouble figuring out the matching and removal of the ".jpg.html" extension if it exists in combination with the /gallery/ -> /photos/ rewrite. The regex matching I believe is going to be .jpg.html to escape the periods, but how do I write rules to remove the ".jpg.html" extension and rewrite the directory?
RewriteRule ^\.jpg\.html$ $1 [NC]
RewriteRule ^(.*)$ /photos/$1 [QSA,L,R=301]
Edit:
Sorry! I neglected earlier to mention the album URL formats can change (doesn't have to specify a photo, and can include sub albums), I've added some specific examples:
The url rewrite rule also needs to follow:
old: http://example.com/gallery
new: http://example.com/photos
old: http://example.com/gallery/album
new: http://example.com/photos/album
old: http://example.com/gallery/album/subalbum/
new: http://example.com/photos/album/subalbum/
old: http://example.com/gallery/album/subalbum/photo.jpg.html
new: http://example.com/photos/album/subalbum/photo
Try:
RedirectMatch 301 ^/gallery(.*?)(\.(jpe?g|gif|png)\.html)?$ /photos$1
Or alternatively using mod_rewrite:
RewriteRule ^/?gallery/([^/]+)/([^.]+)\.(jpe?g|gif|png)\.html$ /photos/$1/$2 [L,R=301]
You don't need the QSA flag as query strings will automatically get appended if you don't have a ? in your rule's target.