I have a double slash in several of my URLs. I've fixed the problem that was causing the double slashes. But now I want to add 301 redirects so Google/Bing etc. know which is the correct page version. I attempted this with the htaccess rule, but my rule doesn't seem to be working. Anything wrong with this htaccess rule? Here it is,
RedirectMatch 301 ^//(.*)$ https://www.example.com/$1
and this is an example of the URL I'm trying to match,
https://www.example.com//faq/
I want this to redirect to,
https://www.example.com/faq/
One thing I noticed was that when I type this URL,
https://www.example.com//faq
It seems to work. Have I written the rule wrong? I'm not the best with these!
Please check this rewrite rule:
RewriteEngine On
RewriteRule ^\/(.*)$ https://www.example.com/$1 [R=301,L]
Related
This is a page on my domain: www.mydomain.com/en/stats.php
I want it to look like this: www.mydomain.com/en/statistics/players
This is pretty simple to accomplish, but for some reason the htaccess code below doesn't work.
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^/statistics/players stats.php [R=301,L,QSA]
I get a 404 error when I try to open the SEO-friendly version. The page opens fine when I use the regular URL. What am I doing wrong?
The URL might not have a leading slash. Try without or optional slash. Additionally, you must check for the leading en, when you anchor your pattern at the beginning
RewriteRule ^/?en/statistics/players /en/stats.php
What worked in the end is the following:
RewriteRule en/statistics/players en/stats.php [NC,L]
I think it needs to be:
RewriteEngine On
RewriteRule ^en/statistics/players en/stats.php [QSA]
The R=301 means it will perform a redirect instead of a rewrite and try to redirect to /stats.php which doesnt exists I guess.
The L flag means that this is the last rewrite rule that will be processed for this request, which in this case I doubt is what you want. If there are rewrite rules further down for /en/stats.php they wont be processed.
I need to redirect some urls.
I need to direct www.site.com/city-st-key-word-string to www.site.com/city-st/key-word-string.
I have about 500 cities I need to do this for. Preferably instead of making a redirect rule, I would like to change the keyword string as well. However, I will end up having 2,500 redirects.
Is it ok to have $2,500 redirects or should I use a redirect rule and if so, what would it be?
Thanks for your help in advance!!!!
Would this keyword string be the same for all cities? If yes, then you could create one rule and change the keyword string in the rule (and it would reflect on all redirects).
Individual rewrite rules would look like this -
Redirect /city-st-key-word-string http://www.site.com/city-st/key-word-string
A single rewrite rule for any city-st would look like this -
RewriteEngine on
RewriteRule ^(.*)/([a-zA-Z]+)-key-word-string$ $1/$2/key-word-string [R=301,L]
A rewrite rule redirecting any /x-x-??? to /x-x/??? (i,e; splitting at the two hyphens) -
RewriteEngine on
RewriteRule ^(.*)/([a-zA-Z]+)-([a-zA-Z]+)-(.*)$ $1/$2-$3/$4 [R=301,L]
Hope this helped :)
Sorry for the long title. I'm currently writing some 301 redirects and using mod rewrite (Apache) to handle them. Currently only one of the two 301s I have tried is working. Here is the code:
#301 REDIRECTS
RewriteEngine On
RewriteRule ^Fox-and-Frank-home\.html$ http://www.mydomain.co.uk/contact_us.php [R=301]
RewriteRule ^about\.html$ http://www.domain.com/about/ [R=301,L]
about.html properly redirects, but Fox-and-Frank-home.html does not. I have tried this with other names, other URLs, but it is not working. Any help will be greatly appreciated.
EDIT
I have gotten this to work on a completely bare .htaccess file. Do 301 redirects need to be at the very top before everything else?
You don't have a L (last) flag on your Fox-and-Frank rewrite, so other rules can potentially be processed, and definetly will be in a .htaccess (where you need to use the END flag).
Try changing it to:
RewriteRule ^Fox-and-Frank-home\.html$ http://www.mydomain.co.uk/contact-us.php [NC,R=301,L]
Where:
NC makes it a case insensitive check (so fox-and-frank-home would
redirect to)
R=301 is the 301 redirect (although you can just put R
and 301 would be assumed I always specify it personally)
L tells
mod_rewrite to stop processing further rules (but as its in a
.htaccess. you may need to use END instead - see http://httpd.apache.org/docs/current/rewrite/flags.html#flag_l).
Regards.
I have done some URL masking and it all works very nicely. I have one issue that I am trying to resolve now:
RewriteRule ^(.*)/(.*)/clubs/(.*)/$ teams.php?competition=$1&season=$2&teamid=$3
with the above I can access the same page 2 ways, www.domain.com/premier-league/2010-2011/arsenal/ and www.domain.com?competition=premier-league&season=2010-2011&teamid=arsenal
is there a way in my rewrite rule I can redirect the URL (301 ideally) is someone does it through the untidy way "www.domain.com?competition=premier-league&season=2010-2011&teamid=arsenal"?
Thanks in advance
If you add another rewrite rule after this one that matches your 'inverse' pattern, and mark both as the [L]ast rule, that might work. I first rewrite the url to include the query string, and [C]hain that one to the next rule. After that we [R]edirect the browser.
RewriteRule ^(.)/(.)/clubs/(.*)/$ teams.php?competition=$1&season=$2&teamid=$3 [L]
RewriteRule ^(.+) $1%{QUERY_STRING} [C]
RewriteRule ^teams.php?competition=([a-zA-Z0-9]+)&season=([a-zA-Z0-9]+)&teamid=([a-zA-Z0-9]+)$ $1/$2/clubs/$3/ [R=301,L]
Note I haven't tested this or the regex of the second rule. Might need to adjust the character ranges a bit. Also I haven't tested the query string rewrite in the second rule.
Edit: See here for some common use cases of mod_rewrite: http://httpd.apache.org/docs/2.0/misc/rewriteguide.html
I'm working in an old CMS that uses htaccess to rewrite URIs with GET variables into something more user friendly.
RewriteEngine on
RewriteRule ^animals/(.*)/ secondary.php?page=$1
RewriteRule ^animals/(.*) secondary.php?page=$1
which results (correctly) in
http://www.example.com/animals/duck
The problem is I now need to redirect some of those pages to new pages. I've tried:
Redirect 301 /animals/goose http://www.example.com/animals/fowl
The redirect almost works, but it adds "?page=goose" to the end of the rewritten URI:
http://www.example.com/animals/fowl?page=goose
I've tried using RewriteRule as well as RewriteCond, but unfortunatley I'm having no luck. Any help would be immensely appreciated.
Try placing this before the other rules instead of the Redirect statement. R=301 is for the redirect and L signals that the rule in question is the last rule to be processed.
RewriteRule ^animals/goose /animals/fowl [R=301,L]
Also you can easily make the slash (just like any other character) optional with a question mark, instead of having two rules.
RewriteRule ^animals/(.*)/?$ secondary.php?page=$1