I have the below rewrite rule and for the life of me can't figure out why it's not working, there are no errors in any logs and nothing being displayed on screen, I'm stumped.
It takes a session token and passes it through the url.
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^/\?action=logout;(.*)$ /forum/index.php?action=logout;$1 [NC,L]
I've also tried this with no luck, no errors or anything to suggest an issue.
RewriteCond %{QUERY_STRING} ^action=logout
RewriteRule ^/\?action=logout;(.*)$ /forum/index.php?action=logout;$1 [L,R=301]
Using QUERY_STRING is a good start.
Anyway, you now need to match / or /index.php in your rule.
Also, since you're passing the same query string, you don't need to capture a part of it. You can use QSA flag instead.
RewriteCond %{QUERY_STRING} ^action=logout [NC]
RewriteRule ^/?(?:index\.php)?$ /forum/index.php [L,NC,R=301,QSA]
If you want a silent redirect (internal rewrite), just remove R=301 flag
Related
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've tried all manor of rules and just can work this one out. If there someone who can solve this?
I need to turn this
http://localhost/search/?terms=foobar
into this
http://localhost/index.php?m=search&terms=foobar
Where "foobar" is really ([^/]*), ie can be anything, it's a search string.
The initial URL has a "?" in it due to the GET method of HTML forms. Although it would be easier without that "?" it's not something that can be done?
I've tried the following with no luck.
#RewriteRule ^search/\?terms\=([^/]*)$ index.php?m=search&terms=$1 [L]
Thanks.
For query string you need to use %{QUERY_STRING}:
RewriteCond %{QUERY_STRING} ^terms=(.*)$ [NC]
RewriteRule ^search/?$ index.php?m=search&terms=%1 [NC,L]
You cannot match QUERY_STRING in RewriteRule. Have your code like this:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^(.+)$
RewriteRule ^(search)/?$ index.php?m=$1 [L,NC,QSA]
Because of QSA flag %{QUERY_STRING} will automatically be carried over to new URL.
So here's what I have.
www.website.com/foo (pretty URL to use on marketing pieces)
www.website.com/foobar (URL that actually exists on site)
I can get www.website.com/foo working perfectly with this:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /foo [NC]
RewriteRule ^ http://www.website.com/redirected/url-goes-here/ [L,R=301]
But that makes the www.website.com/foobar URL go there as well.
I'm sure this is a regex issue and I just don't know the correct symbol to get things working properly, but how can I make /foo redirect properly without effecting /foobar ?
Thanks.
Try this instead:
RewriteCond %{REQUEST_URI} ^/foo$ [NC]
RewriteRule ^ http://www.website.com/redirected/url-goes-here/ [L,R=301]
REQUEST_URI will get rid of the extra request headers that THE_REQUEST has. Then you can match the beginning and end of the requested URL with ^ and $.
You don't need the RewriteCond. Just be specific with the RewriteRule pattern
RewriteRule ^foo$ http://www.website.com/redirected/url-goes-here/ [L,R]
See more about regular expression.
Never test with 301 enabled, see this answer Tips for debugging .htaccess rewrite rules for details.
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 have a simple htaccess redirect to send visitors from website.com/promotions to website.com/go/promotions. If I use the code below I get a redirect loop error:
RewriteEngine on
RewriteCond %{REQUEST_URL} !^go$
RewriteRule promotion/index.html go/promotion/index.html [R,L,NC]
The RewriteCond is supposed to prevent the loop by not redirecting if the request url contains "go" but it doesn't appear to be working.
In addition I'm trying to set up a regex so that promotion/anypage.html will be redirected to go/promotion/anypage.html. I tried the following code but it wouldn't match anything:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^go$
RewriteRule ^promotion/(.*)$ go/promotion/$1 [R,L,NC]
Does anybody have an clues as to why either of these blocks isn't working properly ?
Cheers
%{REQUEST_URL} isn't a var you can match against, you're probably thinking of %{REQUEST_URI}, but it starts with a leading slash so ^go will never match anything.
You just need a single rule:
RewriteEngine On
RewriteRule ^/?promotion/(.*)$ /go/promotion/$1 [R,L]
Or you can use mod_alias:
Redirect /promotion/ /go/promotion/
You're checking if the entire URL is just "go" on it's own, which it's not:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/?go/
RewriteRule promotion/index.html go/promotion/index.html [R,L,NC]