How rewrite a url slug to a parameter using .htaccess? - regex

I'm looking for the regex url rewriting entries for .htaccess - specifically, I'd like a way to re-write:
/site/this-is-my-slug/
to
/site/?slug=this-is-my-slug
Is this possible?

Try this:
RewriteRule ^site/(.*)$ site/?slug=$1 [L,QSA]
The QSA flag will copy over any query string from your original URL request so remove it if you don't want this functionality.

Related

Redirect Query String via .htaccess

I'm trying to redirect the user via the .htaccess file to create friendly URL example:
UI URL: https://example.com/courses/1
htaccess role
RewriteEngine On
RewriteRule ^courses/([0-9]{1})$ /courses.php?page=$1
Output URL: https://example.com/courses.php?page=1
And everything is working fine, Now I need to add other query params to the URL like this http://smart-courses.com/courses/1?p1=1&p2=2 so, I need htaccess to redirect me to https://example.com/courses.php?page=1&p1=1&p2=2
I tried to create a new role to check if p1 and p2 are exists and rewrite the URL
RewriteRule ^courses/([0-9]{1,5})?lid=([0-9]{1,})&did=([0-9]{1,})$ /courses.php?page=$1&p1=$2&p1=$3
Also, I tried to take any chars after ([0-9]{1,5}) (page number) and put it after ?page=1 but it did not worked
RewriteRule ^courses/([0-9]{1})\?(.*)$ /courses.php?page=$1&$2
The query string is not part of the path section of the URL that the rule pattern is matched against. If you'd have to capture something from the query string you need to use a RewritetCond, that is documented. In this case however you don't even need to capture anything:
RewriteEngine On
RewriteRule ^/?courses/(\d)$ /courses.php?page=$1 [QSA]
The QSA flag adds a potential query string to the (rewritten) target URL. The rewriting module takes care to use the correct concatenation here (the & character). Again, this behavior is documented: https://httpd.apache.org/docs/2.4/mod/mod_rewrite.html
Take a look yourself: htaccess.madewithlove.com

How to redirect url with query string to another url with a query string using .htaccess

First, let me start by saying I'm not very skilled with .htaccess. I did search through StackOverflow and found many redirect examples, but couldn't find one to do what I'm trying to accomplish.
I'm trying to redirect specific URLs with query strings to a different URL with a query string. The query string values are dynamic.
For example, I'd like to redirect this:
https://www.example.com/page/?search=search_string&param1=whatever&param2=whatever
To this:
https://www.example.com/?s=search_string
In the first URL, I don't care about any parameters except for the 'search' value. The value for 'search' in the first URL will be the value of 's' in the second URL. I want to drop all other parameters. Keep in mind that the value for 'search' in the first URL is dynamic.
I tried the following with no luck:
RedirectMatch 301 ^/page/?(.*)$ https://www.example.com/?$1
You will need to use mod_rewrite here to be able to examine and capture values from QUERY_STRING.
RewriteEngine On
RewriteCond %{QUERY_STRING} (?:^|&)search=([^&]+) [NC]
RewriteRule ^page/?$ /?s=%1 [L,NC,R=302]

.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

Apache2: Rewrite url containing question mark

I am using Apache 2.4.7. I use mod_rewrite to alter some urls.
I want to rewrite http://example.com/servicename/oldpage?id=abcto http://example.com/servicename/newpage.
Other similar rewrites work so I belive the ? inside url is causing problems.
I have tried escaping it with \.
This works as there is no ? in url:
RewriteRule ^/servicename/old /servicename/new
But these don't work:
RewriteRule ^/servicename/oldpage?id=abc /servicename/newpage
RewriteRule ^/servicename/oldpage\?id=abc /servicename/newpage
I have also tried using RewriteCond from examples like this: .htaccess rewrite URL with a question mark "?" but I didn't manage to get them work.
How should rewrite url that contains question mark?
EDIT: I tried solutions given in Match Question Mark in mod_rewrite rule regex but was not able to make them work for me. That question is about preserving query string when rewrite while I want to remove it when rewriting.
RewriteRule pattern is matched against the part of the URL after the hostname and port, and before the query string.
When the requested URI contains a query string, and the target URI does not, the default behavior of RewriteRule is to copy that query string to the target URI. Using the [QSD] flag causes the query string to be discarded.
So, this should work:
RewriteRule ^/servicename/oldpage /servicename/newpage [QSD]

301 redirect directory with query parameters

I'd like to redirect everything in /bar/ to a new subdomain but still pass in any query parametes from the original URL. E.g
Original URL examples:
http://www.foo.com/bar/
http://www.foo.com/bar/index.php
http://www.foo.com/bar/index.html
http://www.foo.com/bar/test.php?id=123&utm_source=google
Should redirect to:
http://bar.foo.com/
http://bar.foo.com/
http://bar.foo.com/
http://bar.foo.com/?id=123&utm_source=google
This is what I currently have:
RewriteEngine On
RewriteRule ^bar/?(.*)$ https://bar.foo.com/? [R=301,L]
However, it doesn't pass in the query parameters I need in the 4th example above. I know the ? prevents query parameters being passed in so this will most likely need to be removed I assume but not sure what to replace it with?
Cheers
Add a new rule like this:
RewriteEngine On
RewriteRule ^bar(/|$) https://bar.foo.com/ [NC,R=301,L]
QUERY_STRING will automatically be carried over to target URI.
Make sure there is no trailing ? in the target URI which strips existing query string.