I'm trying to do a .htaccess redirect with a parameter but it's not working. Seems like my regex are also wrong. :(
Original URL 1: http://www.example.com/?team=john-doe
Original URL 2: http://www.example.com/?team
Target URL: http://www.example.com/company/
I tried:
RewriteEngine on
RewriteCond %{QUERY_STRING} team=john-doe
RewriteRule /company/ [L,R=301]
Any help would be much appreciated.
Found a generator that works perfectly:
https://donatstudios.com/RewriteRule_Generator
# 301 --- http://www.example.com/?team=john-doe => http://www.example.com/company/
RewriteCond %{QUERY_STRING} (^|&)team\=john\-doe($|&)
RewriteRule ^$ /company/? [L,R=301]
Your RewriteRule is malformed, you are missing a pattern (first argument). Try something like:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^team(?:=john-doe)?
RewriteRule ^$ /company/? [R=301,L]
The RewriteRule pattern ^$ matches requests for the domain root. The ? on the end of the RewriteRule substitution strips the query string from the redirected URL (on Apache 2.4+ you can use the QSD flag instead).
The CondPattern ^team(?:=john-doe)? matches either "team=john-doe" (URL#1) or "team" (URL#2) at the start of the query string. The (?: part just makes it non-capturing.
You will need to clear your browser cache before testing.
Related
So currently I have URLs that looks like this:
http://localhost/?v=register
http://localhost/?v=profile&u=thatgerhard
I want the above to look like this:
http://localhost/register/ (without trailing /)
http://localhost/profile/thatgerhard/ (without trailing /)
I spent a ton of time trying to get this to work even though it seems like it should be a simple fix.
This is what I have atm:
RewriteBase /
RewriteEngine On
RewriteRule ^((.)*)$ /?v=$1&p=$
I would ideally like this to be dynamic so that if you do ?v=foo it will automatically do /foo/ without adding "foo" to the htaccess file.
Any help or direction will be greatly appreciated. :)
You should use RewriteCond Directive and RewriteCond backreferences. The %1 backreference matches the query string parameter v, the %2 backreference matches the query string parameter u. The RewriteRule redirects the request permanently and discard the query string entirely.
RewriteCond %{QUERY_STRING} ^v=([^&]*)$ [NC]
RewriteRule ^/?$ /%1/? [R=301,L]
RewriteCond %{QUERY_STRING} ^v=([^&]*)&u=([^&]*)$ [NC]
RewriteRule ^/?$ /%1/%2/? [R=301,L]
I have tried several examples of how to do this but nothing is working and I fear it is because the query string is part of the base URL.
Here is what I would like to do:
Redirect http://example.com/?from=TEST_1 -> http://example.com/foo
Redirect http://example.com/?from=TEST_2 -> http://example.com/bar
Here is what I have tried:
RewriteEngine On
RewriteCond %{QUERY_STRING} from=TEST_1
RewriteRule (.*) /foo [L,R=301]
Unfortunately the above rule has no impact. If I change the rule to something static (see below) it works great:
Redirect 301 /foo /bar
Is there something special I need to do in order to check the query string value when it's part of the root domain request?
Try these 2 rules as your very first rules just below RewriteEngine line.
RewriteEngine On
RewriteCond %{QUERY_STRING} ^from=TEST_1$
RewriteRule ^/?$ /foo? [L,R=301]
RewriteCond %{QUERY_STRING} ^from=TEST_2$
RewriteRule ^/?$ /bar? [L,R=301]
? at the end of target URI is used to discard previous query string from target.
^/?$ is the regex to match landing page only.
I have this url
http://example.com/profile.php?u=78
I wanted to changed it to be like this
http://example.com/78/
How would I do that?
Please Help
Thanks in advance
You can use this .htaccess in your root directory:
RewriteEngine on
RewriteRule ^(\d+)/?$ /profile.php?u=$1 [L]
RewriteCond %{QUERY_STRING} ^u=(\d+)$
RewriteRule ^profile.php$ /%1/? [R=302,L]
Change [R=302,L] to [R=301,L] when that work well
Try this to rewrite http://example.com/profile.php?u=78 into http://example.com/78/:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(\w+)=(\w+)$
RewriteRule ^profile.php$ /%2/
RewriteRule (.*) $1? [R=permanent]
Line-by-line explanation:
Turn on rewriting functionality
Specify the condition to make sure the query string is in the form of foo=bar for the following rules to apply.
Rewrite profile.php?foo=bar into /bar/?foo=bar
Remove all query strings that may still be appended to the new URL like /bar/?foo=bar/. This rule essentially substitute the entire path (.*) with itself ($1), and emptying the query string (?). [R=permanent] performs a permanent redirect (301).
Also, as previously commented, please ensure that http://example.com/78/ is a valid URL.
I am having problems understanding the rewriting rules. Basically what I am trying to do is change this:
http://domain.com/Intern/guangjian/gallery/?page=admin_login
Into this:
http://domain.com/Intern/guangjian/gallery/admin_login
I keep getting 500 Internal Server Error when I applied various rules to my .htaccess file. Please help. Currently I have the below:
RewriteEngine ON
RewriteCond %{QUERY_STRING} ^(\w+)=(\w+)$
RewriteRule ^/%2
I admit I don't really understand what all the symbols mean.
The symbols are converting the characters into a sequence of bytes in a specific set using character encoding
Example
Original URL.
Eg. http://flickr.com/users.php?id=username&page=2
Rewriting Friendly URL.
Eg. http://flickr.com/username/2
//First Parameter
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ users.php?user=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ users.php?user=$1
//Second Parameter
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)$ users.php?user=$1&page=$2
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)/$ users.php?user=$1&page=$2
Place this code in /Intern/guangjian/gallery/.htaccess file:
RewriteEngine On
RewriteBase /Intern/guangjian/gallery/
RewriteCond %{THE_REQUEST} /\?page=([^\s&]+) [NC]
RewriteRule ^ %1? [R=302,L]
RewriteRule ^([^/.]+)/?$ ?page=$1 [L,QSA]
Rewrite Rule syntax is: RewriteRule Pattern Substitution [flags].
This means only the flags (L, NC, R, etc...) are optional, the pattern to match and the substitution parts are mandatory. But you only specified one, which is why you are getting a 500 Internal Server Error.
You don't need RewriteCond. Try this:
RewriteEngine ON
RewriteRule ^(.*)\?(\w+)=(\w+)$ $1/$3
You are getting everything up to the ?, and then attaching the value of page, or whatever argument of the query string, to the url. This of course only works when you have only one argument in the query string, the regex won't match in other case.
I've been trying to rewrite a URL such as
www.somesite.com/?x=372
into a url
www.somesite.com/
My current code does not seem to work
RewriteEngine On
RewriteCond %{QUERY_STRING} x=(.*)
RewriteRule http://www.somesite.com/ [R=301,L]
I've looked up countless ways of trying to do it with htaccess and still no success.
If you simply want to redirect a client to remove the query string (everything after the ? in the URL), then you can try this:
RewriteEngine On
RewriteCond %{QUERY_STRING} x=(.*)
RewriteRule ^ http://www.somesite.com/? [R=301,L]
You've got most of it right, it seems, but your rule needs to have a match, and your target (the http://www.somesite.com/) needs a ? at the end so that any query string before the rewrite won't get appended.
In Apache 2.4 or newer you can use the QSD query string discard flag:
RewriteEngine On
RewriteCond %{QUERY_STRING} x=(.*)
RewriteRule .* http://www.somesite.com/ [R=301,L,QSD]