I want to rewrite two old urls:
www.example.com/Car-Systems/ford.html
www.example.com/mobile/new-car-systems/ford.html
New target url:
www.newexample.com/new-car-systems/ford.html
One rewrite is actually pretty simple:
RewriteRule ^www\.example\.com/Car-Systems/ford\.html$ /www.newexample.com/new-car-systems/ford.html? [L,R=301]
But how do I get the second one in only one rewrite? Or do I always have to create 2 rules for each rewrite?
The single rule that you have wouldn't work either, unless you're visiting those pages as: http://some.domain.com/www.example.com/mobile/new-car-systems/ford.html. Try:
RewriteEngine On
RewriteRule ^(mobile/new-)?car-systems/ford.html$ /new-car-systems/ford.html [NC,L,R=301]
Related
I'm having an issue with some htaccess rules which I thought would be simple. I have some nice SEO friendly URL rewriting in place as below
RewriteEngine On
RewriteCond %{REQUEST_URI} !^(index\.php|/images|/templates|/views|/ajax|/uploads|/robots\.txt|/sitemap\.xml|/favicon\.ico|/scripts|/cron|/combine.php|/js|/css)
RewriteRule ^(.*)$ index.php?ref=$1&%{QUERY_STRING} [L]
This all works well and I want to keep this. I also wish to rewrite some old pages which Google WMT is reporting as 404's to the new equivalent and for that I'd like to use:
Redirect 301 /about_us http://example.com/about-us
The problem I have is that the URL that the browser is directed to is:
http://example.com/about-us?ref=about_us
The about_us is the old link and about-us is the correct link. If the htaccess redirected to example.com/about-us then the other SEO friendly rewrite rule will pick it up and show the page but eh extra ?ref= parameter is confusing it. I am guessing the two rules are conflicting to a degree but is there a way to get the two rules to work together e.g. redirect without the extra ?ref= parameter? My knowledge of htaccess is basic to say the least so I am a little stuck on this one.
Thanks in advance
Redirect and RedirectMatch are part of mod_alias, while the rewrite rules are part of mod_rewrite. The problem you're running into is when you mix the two, both modules affect the same request, thus two things happen when you only want one. In this case, you need to stick with just mod_rewrite and use this instead:
RewriteEngine On
RewriteRule ^about_us /about-us [L,R=301]
RewriteCond %{REQUEST_URI} !^(index\.php|/images|/templates|/views|/ajax|/uploads|/robots\.txt|/sitemap\.xml|/favicon\.ico|/scripts|/cron|/combine.php|/js|/css)
RewriteRule ^(.*)$ index.php?ref=$1&%{QUERY_STRING} [L]
Note that the rule that redirects comes before the rule that routes to index.php.
Have a page currently with the URL /results-details.php?mls_number=stringofnumbers.
I want it to re-write to: /results-details/stringofnumbers
However I want that to basically resolve back to the original page.
I'm also changing all the URLs on the site internally to point to the URL. So I have two re-write rules:
RewriteCond %{QUERY_STRING} mls_number=([0-9]+)$
RewriteRule (.*) /new-homes/results-details/%1? [R=301,C]
RewriteRule ^results-details/([0-9]+)$ results-details.php?mls_number=$1
The second rule works fine on it's own with internal links in the /results-details/stringofnumbers form, but the first one doesn't chain properly to the second one and not sure what I'm doing wrong.
Basically trying to retain any links to the old URLs that might be out there but start using the new URS internally.
Suggestions?
YOu need to match against the actual request and not the URI, because the rewrite engine loops:
RewriteCond %{THE_REQUEST} \ /new-homes/results-details\.php?mls_number=([0-9]+)
RewriteRule ^ /new-homes/results-details/%1? [L,R=301]
RewriteRule ^results-details/([0-9]+)$ results-details.php?mls_number=$1
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 :)
I am trying to figure out how to do a 301 redirect in my htaccess file to redirect some files to a new domain. Here's what I need to know how to do:
OLD URL: http://www.example.com/index.php?page=news&id=2366
NEW URL: http://www.example2.com/news.php?name=23546
The redirects don't have to be automatically created. I can hard-code the pages I need to redirect in the htaccess file, I just don't know the format to use as I've read that a "standard" 301 redirect won't work with query strings.
Basically this is what I want to do, but from my research so far it doesn't sound like it can be done this way.
redirect 301 /index.php?page=news&id=2366 http://www.example2.com/news.php?name=23546
You could use a rewrite rule with a query string match condition, such as:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/index.php$
RewriteCond %{QUERY_STRING} ^page=news&id=2366$
RewriteRule ^(.*)$ http://www.example2.com/news.php?name=23546 [R=301,L]
Checkout this blog page for more information on how this works.
I had the same problem, but still more complicated, because I needed to discard other parameters.
Like this: my-old-page.php?a=1&b=2&c=3
I need to use one of the strings and discard the others, but that solution only worked if I want to use the last parameter (c=3). If I want to use any other (a=1 or b=2) it runs to a 404. After much struggling and searching, I found an answer:
RewriteCond %{QUERY_STRING} ^.* ?b=2.* ?$ (without space after the *)
RewriteRule (.*) http://www.my-webpage.php/new-location-2? [R=301,L]
The solution is to add ".*?" before and after the parameter to use.
I don't know if this is the best solution, but it works.
I've got a .htaccess file that has got a rewrite rule in it as follows which works fine:
RewriteRule ^solicitorsin([^/]+)/all/([0-9]+)$ /search/searchresults.php?county=$1&page=$2 [L]
What I'm looking to do is to keep using this for if the page variable is 2 or higher, but if it's 1 I want to 301 redirect to a separate url (the same site) say http://www.domain.com/solicitorsinCOUNTY/
The problem is that if I try doing this using a 301 redirect or a rewrite rule it still performs the above rewrite rule as well so I end up with http://www.domain.com/solicitorsinCOUNTY/?county=COUNTY&page=1
I haven't done much with .htaccess before so I'm not even sure if this is possible, can anyone help please? It would be much appreciated.
If you are using a rewrite rule, then put the rule for page=1 above the other rule and make sure you have the [L] flag.
Alternatively, you can use RewriteCond to prevent the rule from being run on specific URLS like this:
RewriteCond %{REQUEST_URI} !^solicitorsin([^/]+)/all/1$
RewriteRule ^solicitorsin([^/]+)/all/([0-9]+)$ /search/searchresults.php?county=$1&page=$2 [L]