I've a WP site and I changed the permalinks, but to keep google indexed urls working I tried to set 301 redirect rules. That works but I now have the issue that the category pagination doesn't work anymore. So I need to add a exception to the condition, but how do I do that?
Current rewrite rule is:
RewriteRule ^category/blog/(.*)$ /$1 [R=301,NC,L]
But I need it to be ignored when $1 contains page/ so urls like category/blog/page/2 can pass.
How should I do this?
You can use:
RewriteCond $1 !page [NC]
RewriteRule ^category/blog/(.*)$ /$1 [R=301,NC,L]
Related
I'm trying to redirect http requests that contain a specific URI to a different domain with a different URI completely. Redirecting the top level domain works but I can't seem to get the URI rules to redirect.
In essence it should act as follows:
If the url request is:
www.example.com/unique-URI
it needs to redirect to:
https://example2.com/anotheruniqueURI
Currently I have this:
RewriteEngine On
#This redirect works successfully
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^(.*)$ http://example2.com/something [R=301,L]
#This attempt to redirect requests with the specific URI does not work.
RewriteCond %{HTTP_HOST} ^www\.example\.com
RewriteCond %{REQUEST_URI} ^/cars-application$ [NC]
RewriteRule ^/(.*)$ https://example2.com/anotherURI/ [R=301,NC,L]
I've tried many different combinations inside my RewriteRule such as explicitly stating the URI like I did in the RewriteCond above but that doesn't work. Using $1 here won't apply since I'm redirecting to a completely different domain and URI. The URI's I am expecting will be unique. Could you guys provide me some pointers. Is my regex correct or is my rewrite rule capture just wrong?
Your rule failed to work due to the leading slash in your RewriteRule's pattern . Remove the slash to fix it.
RewriteRule ^(.*)$ https://example2.com/anotherURI/ [R=301,NC,L]
Assuming you are redirecting from within a virtualhost of the first domain, you may just do the following:
Redirect permanent /unique-URI http://www.domain2.com/newlocation
I'm trying to alter a part of an URL as below from this type URL ...
/shop/product/41/3030651/Apple-Imac-215-Inches-Me086ba-By-Viking.html
to this ..
/shop/product/1/030651/Apple-Imac-215-Inches-Me086ba-By-Viking.html
where all numbers like 41 are altered to 1 but it seems this code ...
RewriteRule ^product/([^/]+)/([^/]+)/([^/]+)\.html$ index.php?case=product&proddb=$1&pid=$2&urltxt=$3 [L]
is interfering.
I've tried lots of rewrite configurations but nothing seems to work but I do get a reaction from the code below ...
RedirectMatch 301 /shop/product/41/(.*) /shop/product/1/$1
But it must conflict with the RewriteRule above as I get sent to the root of the site.
Have redirect rule before your earlier rewrite rule:
RewriteEngine On
RewriteRule ^product/41/(.+)$ /product/1/$1 [L,NC,R=301]
RewriteRule ^product/([^/]+)/([^/]+)/([^/]+)\.html$ index.php?case=product&proddb=$1&pid=$2&urltxt=$3 [L,QSA,NC]
I am implementing a 1:1 redirect in htaccess with something like this:
Redirect 301 /1.html site.com/folder/1
Redirect 301 /2.html site.com/2
Redirect 301 /3 site.com/vw-vans/another-folder/2
Redirect 301 /3.html site.com/3
RewriteCond %{REQUEST_URI} .*\.(html)
RewriteRule ^(.+\.html)$ site.com/folder? [R=301]
What I was expecting here was if 1.html matches it will be redirected to site.com/folder/1 and not site.com/folder. But unless I remove the rewrite rule, that is what is happening. Even with a 'L' flag.
Is it not possible or am I doing something wrong if I want the 1:1 redirect to work and if an url is not part of those 1:1 redirects, it will then apply the rewrite condition. I don't think there is a [L] flag for the 'Redirect' method.
Don't mix mod_alias rules and mod_rewrite one as they both get invoked at different times by Apache.
Try this in your .htaccess:
RewriteEngine On
RewriteRule ^1\.html$ /folder/1 [R=301,L,NC]
RewriteRule ^2\.html$ /2 [R=301,L,NC]
RewriteRule ^3\.html$ /3 [R=301,L,NC]
RewriteRule ^3/?$ /vw-vans/another-folder/2 [R=301,L,NC]
# catchall rule for .html files
RewriteRule ^.+?\.html$ /folder? [R=301,L,NC]
Make sure to clear your browser cache before testing this.
I need some help trying to redirect a query to a url.
www.example.com/?categoryID=79
needs to redirect to
www.example.com/catname
where catname is just a string, it has no variables.
Here's what I tried so far:
I began with a humble approach that failed horribly:
redirect 301 /?CategoryID=79 http://www.example.com/catname/
then i moved on to mod_rewrite :
RewriteCond %{QUERY_STRING} CategoryID=79$
RewriteRule (.*) /catname/? [R=301,L]
Both did not work and I'm actually stomped.
any help would be appreciated.
Just to be clear, In the end i'll have many of these rules redirecting to various category names.
important - it's not enough for /catname to display the proper page, the request with the query parameter must redirect to the new url.
This rule should work for you as first rule in your .htaccess:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+\?CategoryID=79[&\s] [NC]
RewriteRule ^ /catname/? [R=302,L]
It's my first request here and hopefully I won't upset anyone.
Here's my story:
I have looked all over this place for a solution and wasn't able to find one. Here's hoping that someone can give some input.
I've basically managed to use Apache's mod_rewrite to create SEO-Friendly urls for my website.
E.g. Old path www.hostname/index1.php?session=user is now rewritten as www.hostname/user, which results into a SEO Friendly URL address.
However, the old path is still valid. I need to somehow redirect all the incoming old index1.php requests to the newer URLs, the SEO-Friendly ones, for the search engines to transfer the link popularities to the new ones. I believe I may have an infinite-loop redirect and that's why it's not working.
My code so far:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
## Redirect still valid old links to SEO friendly ones
RewriteRule ^/?index1\.php\?session=user$ /user [R=301]
## Catch the above and rewrite the URL
RewriteRule ^/?user/?$ /index1.php?session=user [QSA,L]
The above rules never get hit when the htaccess file is parsed.
It hit me that I might be doing some sort of redirect loop here so I thought about renaming the index1.php file to index2.php and create something like:
## Redirect still valid old links to SEO friendly ones
RewriteRule ^/?index1\.php\?session=user$ /user [R=301]
## Catch the above and rewrite the URL
RewriteRule ^/?user/?$ /index2.php?session=user [QSA,L]
However, that failed too.
What would be the best approach to this? What am I doing wrong here?
Thank you!
Update your .htaccess rules to
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
## Redirect still valid old links to SEO friendly ones
RewriteCond %{QUERY_STRING} !no-redir [NC]
RewriteCond %{QUERY_STRING} session=user [NC]
RewriteRule ^/?index1\.php$ /user? [R=301,NC,L]
## Catch the above and rewrite the URL
RewriteRule ^/?user/?$ /index1.php?session=user&no-redir [QSA,NC,L]
You can't match against the query string (everything after the ?) in a rewrite rule, so you can't match against the session= part. You also can't simply match against the %{QUERY_STRING} var because that gets populated by you other rule when it rewrites the SEO friendly URL to the one with the query string. So you need to match against the actual request:
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /index1\.php\?session=([^&]+)&?([^\ ]*)
RewriteRule ^ /%2?%3 [L,R=301]