I'm trying to redirect everything under www.mywebsite/asd/ except for three URLs and any subcontent, www.mywebsite/asd/A/, www.mywebsite/asd/B/ and www.mywebsite/asd/A/.
I tried the following pattern, but it seems to be faulty:
RedirectMatch 301 ^/asd/((?!A/|B/|C/).)* /asd/new-target/index.html
The regular expression does what I want, when I test it in a regex editor. But in htaccess it doesn't work. What do I have to change?
You can use this rule:
RedirectMatch 301 ^/asd/(?!A/|B/|C/|new-target/)(.*)$ /asd/new-target/index.html
And test it after clearing your browser cache. Make sure this is your first rule in site root .htaccess and there is no .htaccess in /asd/ directory.
Alternatively you can use this mod_rewrite rule in your site root .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/ads/(?:A|B|C|new-target)/ [NC]
RewriteRule ^/?asd(?:/.*)?$ /asd/new-target/index.html [L,NC,R=301]
Related
I've moved many url in my site from: /folder/page.php to: /folder/page/.
Now I want to redirect old url to new url, so I added in my .htaccess this rule:
RedirectMatch permanent /folder/(.*)\.php https://www.site.it/folder/$1/
But it doesn't work, this rule first redirect to /folder/page/ and then redirect again to /folder/page/index/
So I tried this:
RedirectMatch permanent /folder/(?!index)\.php https://www.site.it/folder/$1/
But it works like the above rule.
I tested also:
RedirectMatch permanent /folder/(?!index)(.*)\.php https://www.site.it/folder/$1/
same again.
I use .htaccess only in the root folder, here is the content before the new rule:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
These rules are required to redirect http traffic to https and are suggested by the web hosting service I use.
I also tested wrong RedirectMatch rule without any other.
How can I redirect all page except index.php?
Here is the list of url and the desired behavior:
/folder/ not match no action
/folder/index.php not match no action
/folder/subfolder/ not match no action
/folder/subfolder/index.php not match no action
/folder/anindex.php match redirect to /folder/anindex/
/folder/indexfile.php match redirect to /folder/indexfile/
/folder/anindexfile.php match redirect to /folder/anindexfile/
/folder/anotherfile.php match redirect to /folder/anotherfile/
Please do not suggest to use mod_rewrite because it is not the best practice as described here.
You may add a new rule below your existing rule:
RewriteCond %{THE_REQUEST} \s(/folder/[\w-]+)\.php[?\s] [NC]
RewriteRule ^ %1/ [L,R=301]
THE_REQUEST matches only original URI sent by client, it won't match modified REQUEST_URI.
I found this question: Match everything except for specified strings that help me to fix regex.
RedirectMatch permanent /folder/(?!.*(index))(.*)\.php https://www.site.it/folder/$2/
This rule match everything except index so redirect /folder/page.php only once to /folder/page/ but unfortunately, as #anubhava pointed out, this rule doesn't match any page name that contains index.
After further dig, I found this solution:
RedirectMatch permanent /folder/(?!index.php$|$)(([\w-]+)\.php$) https://www.site.it/folder/$2/
Anyway thanks to #anubhava that support me.
http://www.matthewsvolvosite.com/for...p?f=11&t=17479
Need to match and 301 redirect that. I've tried
RedirectMatch 301 ^/for\.\.\.p?f=11&t=17479$ /forums/viewtopic.php?t=17479
and
RedirectMatch 301 ^/for(.*)f=11&t=17479 /forums/viewtopic.php?f=2&t=17479
Neither work. I've searched and tried many examples. I'm at a loss.
The .htaccess file does match and redirect many other strings.
RewriteBase /
is active and working.
You need to use mod-rewrite
RewriteEngine on
RewriteCond %{QUERY_STRING} ^p=11&t=17479$
RewriteRule ^for\.\.\.p$ /forums/viewtopic.php?t=17479 [NC,L,R]
The reason why your redirect is not working is because you are using mod-alias to handle query strings .
Suddenly GWT has reported over 50 404 errors for urls of this type
http://example.com/abc/&sa=U&ved=0CCYQFjADahUKEwi_lo-6nInHAhXIoZQKHfkXDZw&usg=AFQjCNEfkaKtU35GolQ-KLlTBjIuoMejlQ
I want to redirect these ugly and irrelevant urls to the actual URL i.e http://example.com/abc/
Will this code do the job
RedirectMatch ^/&sa$ /$1? [R=301,L]
or should i use query string for the same??
You can use this rule in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteRule ^([^&]+)& /$1? [L,NE,R=302]
I have the following pages after enabling pretty urls in my CMS:
www.mydomain.com/cars/
www.mydomain.com/cars/carshome/
www.mydomain.com/cars/honda/
www.mydomain.com/cars/toyota/
...
I want all requests to www.mydomain.com/cars/ to go to www.mydomain.com/cars/carshome/. Thus I've been trying to use in Apache .htaccess file (after pretty urls code executes):
Redirect 301 /cars https://www.mydomain.com/cars/carshome/
and the result is it goes in infinite loop:
(https://www.mydomain.com/cars/carshome/carshome/carshome/...).
How to resolve?
A second question would be, assuming the solution above needs to be changed here: how to redirect the other direction (from www.mydomain.com/trucks/truckshome/ to www.mydomain.com/trucks/)?
Instead of Redirect you can use RedirectMatch directive for its regex capability:
RedirectMatch 301 ^/cars/?$ https://www.mydomain.com/cars/carshome/
This should work for you:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/cars/carshome/(.*)
RewriteRule ^cars/(.*)$ /cars/carshome/$1 [R=301,L]
The above will check to see if the url contains cars/carshome/ and if it does, then nothing happens, but if it doesn't then it will redirect.
As for the second question, this should work:
RewriteCond %{REQUEST_URI} ^/trucks/truckshome/(.*)
RewriteRule ^trucks/truckshome/(.*)$ /trucks/$1 [R=301,L]
I am not so well versed with regular expressions and .htaccess rewrites and redirects. I have the following :
www.site.com/subDomainName
and I want it redirected to
subDomainName.site.com.
I was able to get some code to do the redirect but it kept being pointed to www.subDomainName.site.com.
Try adding these rules to the htaccess file in your document root:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^subDomainName\.site\.com$ [NC]
RewriteRule ^subDomainName(.*)$ http://subDomainName.site.com$1 [L,R]
To make the redirect permanent, change the R flag in the square brackets to R=301.