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.
Related
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.
I'm having trouble redirecting URLs like site.com/?var=abc to site.com/file.php?var=abc.
I tried a lot of redirect codes but none worked. I installed Wordpress in the root directory of the site and I'm trying to move the old content based on GET variables to file.php.
Thank you!
To redirect every empty url to file.php, here is what you need to write in your .htaccess:
RewriteRule ^/?$ file.php [R=301,QSA,L]
[QSA] tag will append your query string (?var=abc) to the new url.
To redirect the exact format /?var=abc, you'll need to use %{QUERY_STRING}:
# Check for query string, must be exactly "?var=abc"
RewriteCond %{QUERY_STRING} ^var=abc$
# Check for url, must be "/" or ""
RewriteRule ^/?$ file.php [R=301,QSA,L]
Solved by adding (.*)$ after the first variable (var), there were many other parameters in the link.
RewriteCond %{QUERY_STRING} ^var=(.*)$
RewriteRule ^/?$ /file.php$1 [R=301,QSA,L]
I have this dynamic link:
http://www.nortedigital.mx/article.php?id=36175&t=dobla_las_manos_el_snte__avala_reforma_educativa
and I need to convert in URL friendly like this:
http://www.nortedigital.mx/36174/se_enriquecio_elba_en_sexenios_del_pan.html
and i have this RewriteRule:
RewriteRule ^([^/]*)/([^/]*)\.html$ /article.php?id=$1&t=$2 [L]
but doesn't work. Please, anybody can help me?
You must capture the query string in a RewriteCond and use that in the RewriteRule
RewriteEngine On
RewriteCond %{QUERY_STRING} ^id=(\d+)&t=(.+)$
RewriteRule ^/?article\.php$ /%1/%2.html? [R,L]
This redirects the client to request i.e. /36174/se_enriquecio_elba_en_sexenios_del_pan.html. Now you must server the real page. For that, we add an additional rule, similar to the one you already have in your question
RewriteRule ^/?(.+?)/(.+?)\.html$ /article.php?id=$1&t=$2 [L]
But now, there's an endless redirect loop. We break this by using an environment variable. Here is the whole complete ruleset
RewriteEngine On
RewriteCond %{ENV:REDIRECT_SEO} ^$
RewriteCond %{QUERY_STRING} ^id=(\d+)&t=(.+)$
RewriteRule ^/?article\.php$ /%1/%2.html? [R,L]
RewriteRule ^/?(.+?)/(.+?)\.html$ /article.php?id=$1&t=$2 [L,E=SEO:1]
This rule does the redirect as above, as long as the environment variable is not set. And it serves the real page from article.php and sets the environment variable at the same time to prevent the loop.
You can use cookies for this purpose too. But that will break, if cookies are disabled in the client.
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]
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !^category?$
RewriteRule ([^/]+)/([^/]+)/([^/]+)/$ http://domain.com/$3/ [L,R=permanent]
Currently I have the following redirection and it is working like a charm. Now I want to make sure that the link does not begins with /category/ therefore I have inserted the condition. Unfortunately it does not seems to work. Please help. Thanks.
Another question is, how to make that the end permalink that is between the slash is selected to be redirected only. For example, I may have links like http://domain.com/downloads/26-fine-wallpapers/ and http://domain.com/downloads/icons/35-nice-icons/ and I want links like these to be redirected to http://newdomain.com/35-nice-icons/ and http://newdomain.com/26-fine-wallpapers/
I am using wordpress actually.
According to your description you only have two path segments. So your pattern should be:
RewriteRule ^([^/]+)/([^/]+)/$ http://example.com/$3/ [L,R=permanent]
And to exclude /category/…, you can either check the request URI path in REQUEST_URI:
RewriteCond %{REQUEST_URI} !^/category/
RewriteRule ^([^/]+)/([^/]+)/$ http://example.com/$3/ [L,R=permanent]
Or you check the matched value of the first group:
RewriteCond $1 !=category
RewriteRule ^([^/]+)/([^/]+)/$ http://example.com/$3/ [L,R=permanent]
I think you just need a prefixing /:
RewriteCond %{REQUEST_URI} !^/category?$