I have the below code in my htaccess file:
RewriteCond %{REQUEST_URI} !\.(gif|css|png|js|jpg|jpeg|html)$ [NC]
RewriteCond %{REQUEST_URI} !(\.gif|\.jpg|\.png)$ [NC]
RewriteRule ^deals/(.+)/(.+)/$ deals.php?make=$1&model=$2 [L,QSA]
This means I have SEO friendly urls - rather than going to:
URL.com/deals?make=Samsung&model=500CC (Old SEO unfriendly URL)
I can use:
URL.com/deals/Samsung/500CC/
The issue I have is if a user has bookmarked one of my old urls, it does not automatically redirect them to the new style. I have tried adding R=301 but that takes users from the new url to the old.
I've then tried switching them around with the R-301 in place but that still does not work for me.
Appreciate if somebody can help me get any hits to my old url, redirected to the new SEO friendly structure.
You can have another rule to redirect old URL to new one:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+deals(?:\.php)?\?make=([^\s&]+)&model=([^\s&]+) [NC]
RewriteRule ^ /deals/%1/%2? [R=302,L]
RewriteCond %{REQUEST_URI} !\.(gif|css|png|js|jpg|jpeg|html)$ [NC]
RewriteRule ^deals/(.+)/(.+)/$ deals.php?make=$1&model=$2 [L,QSA]
Related
I have a client project on search engine marketing with a website https://www.iehsacademy.com/,
The site is 13 years old. before the client was using the URL structure like with HTML like this: https://www.example.com/?index.html (Home Page),
https://www.example.com/?cndc.html,
But last year they completely remake the website URL structure and make it to https://www.example.com/ (Home Page), https://www.exsample.com/about. https://www.example.com/contact.
Now, The old URLs and the new URLs are both working. The client wants and also for SEO purposes we want to block the old URL or redirect them to the new URL. I tried many rewrite conditions on htaccess
RewriteRule ^([a-z]+).html https://www.iehsacademy.com/ [QSA,L]
This one also
RewriteCond %{HTTP_HOST} ^www.iehsacademy.com/?cndc.html$
RewriteRule (.*)$ http://www.iehsacademy.com/$1 [R=301,L]
Not Working anything. The client's developer is also confused. Can anyone help with this or where i am doing wrong?
NB: I took permission from the site owner to share the client's URLs and problems.
You can use the following redirection rule :
RewriteEngine On
#redirect /?index.html to /
RewriteCond %{HTTP_HOST} ^www.iehsacademy.com$ [NC]
RewriteCond %{QUERY_STRING} ^index\.html$
RewriteRule ^$ https://example.com/? [L,R=301]
#redirect /?foobar to /foobar
RewriteCond %{HTTP_HOST} ^www.iehsacademy.com$ [NC]
RewriteCond %{QUERY_STRING} ^(.+)$
RewriteRule ^$ https://example.com/%1? [L,R=301]
Make sure to clear your browser caches or use a different browser to test the redirection. And do not forget to replace the destination domain example.com with your domain name.
I need help to write proper rewrite rules in my htaccess files.
I need to redirect something like fr.example.com to example.com/fr, because we recently changed the whole website and the multilingual system is managed differently. The structure and the pages too.
I managed to do that successfully with this piece of code:
RewriteCond %{HTTP_HOST} ^fr\.example\.com [NC]
RewriteRule (.*) http://example.com/fr/$1 [L,R=301]
My problem now is to write something more specific for pages, for example :
fr.example.com/discover/foo should go to example.com/fr/bar/foo (different path, nothing consistant)
BUT ! example.com/discover/foo should go to example.com/bar/foo (end of the url is the same in both english and french)
Right now, since I have some common 301 redirects, the french urls aren't redirect properly and lead to the english pages. For example that one :
Redirect 301 /discover/foo /bar/otherfoo
Successfully redirects example.com/discover/foo to example.com/bar/otherfoo but also redirects fr.example.com/discover/otherfoo
How can I write two different rules for english and french? I'll have to write a bunch of different rules since everything is very different from the old subdomain to the new directory, I don't mind.
Thanks !
EDIT
Please note that it's for a wordpress installation, and the htaccess starts with :
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
First the these rules:
RewriteCond %{HTTP_HOST} ^fr\.example\.com [NC]
RewriteRule (.*) http://example.com/fr/$1 [L,R=301]
should look like this :
RewriteCond %{HTTP_HOST} ^(www\.)?fr\.example\.com [NC]
RewriteRule (.*) http://example.com/fr/$1 [L,R=301]
In order to capture bot www & non-www requests for subdomain.
Also this rule :
Redirect 301 /discover/foo /bar/foo
Will capture both requests to domain and sub-domains and using mod_rewrite here is correct not mod_alias so , replace this line with :
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com [NC]
RewriteRule ^discover/foo http://example.com/bar/foo [L,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?(fr)\.example\.com [NC]
RewriteRule ^discover/foo http://example.com/%2/bar/foo [L,R=301]
Note: clear browser cache then test.
So I need to redirect a mass of old Wordpress short urls that begin with question marks such as:
/?post=731
Or
/?cat=73
I need them to be instead /page
I tried
RewriteRule /?cat=73$ http://www.mydomain.com/page? [R=301,L]
Didn't work
I tried
RewriteCond %{QUERY_STRING} ^cat=73$
RewriteRule ^/$ page? [L,R=301]
Didn't work.
Each old URL will have to be manually redirected to a new url. Meaning:
/?cat=73 goes to /category-73
/?cat=2 goes to /main-category
/?post=731 goes to /this-page
These are just examples.
Each old link will have a NEW link - no one size fits all.
I have about 500 URLs like this. Anyone got any good ideas?
Try this rule:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^cat=73$ [NC]
RewriteRule ^ /category-73? [L,R=301]
RewriteCond %{QUERY_STRING} ^cat=2$ [NC]
RewriteRule ^ /main-category? [L,R=301]
I have a domain whhich i want to redirect to a new domain. I want the request to stay the same but my following code is adding the .php extention to it. The following code is what i have:
RewriteCond %{HTTP_HOST} ^oldomain.eu [NC]
RewriteRule ^(.*)$ http://newdomain.eu/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^www.olddomain.eu [NC]
RewriteRule ^(.*)$ http://newdomain.eu/$1 [L,R=301]
When i go to the following url: www.oldomain.eu/samplepage it is redirecting me to www.newdomain.eu/samplepage.php
How could i prevent this from adding the .php to the url?
This sounds like mod_negotiation interferring. Try adding a:
Options -Multiviews
above your rules.
I have a site that has been up for some time. I had a blog on a subdomain for some time. I have decided to do away with the main site and just support the blog subdomain.
I have a redirect setup for this, but it carries all the extra parameters through to the blog which results in a file not found page appearing. I just want the redirect to go to the index page without parameters.
What I currently have in my .htaccess file is this
RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^/?(.*)$ "http\:\/\/blog\.foo\.org\/index\.php" [R=301,L]
When I get a request to
http://www.foo.org/foo/foo/?module=foo
it redirects to
http://blog.foo.org/foo/foo/index.php?module=foo
I want it to redirect to
http://blog.foo.org/index.php
You have to specify the query in the replacement to override the original:
RewriteCond %{HTTP_HOST} !^blog\.example\.org$ [NC]
RewriteRule ^ http://blog.example.org/index.php? [R=301,L]
RewriteEngine On
Options +FollowSymlinks
RewriteCond %{HTTP_HOST} ^(www\.)?foo\.org$ [NC]
RewriteRule ^ http://blog.foo.org/ [R=301,L]