Alter section of URL using htaccess - regex

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]

Related

Confusion on Htaccess redirect

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]

Catchall rewrite rule after 1:1 301 redirect stops 1:1 redirect from working

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.

short links with htaccess - two arguments

I've got little problem that I am not sure how to fix. I am using htaccess file to rewrite my url to shorter links. For example I am using this rule:
RewriteEngine On
RewriteRule ^client/(.*)/?$ pages/client.php?action=$1 [L,QSA]
but I wanted to also pass second argument to that URL so I made this:
RewriteRule ^client/(.*)/(.*)/?$ pages/client.php?action=$1&view=$2 [L,QSA]
so I could access URL like:
/client/action_1/view_2
without any problems but when I skipped second argument and only accessed:
/client/action_1/
then it was impossilbe and resulted in 404.
How to make a change to that RewriteRule so I can access both:
/client/action_1/view_2
/client/action_1/
and it would rewrite to:
pages/client.php?action=$1&view=$2
You can have 2 separate rules to handle 2 clean URLs:
RewriteEngine On
RewriteRule ^client/([^/]+)/?$ pages/client.php?action=$1 [L,QSA,NC]
RewriteRule ^client/([^/]+)/([^/]+)/?$ pages/client.php?action=$1&view=$2 [L,QSA,NC]

Mod_rewrite - redirecting an already rewritten url

I had previously rewritten the URL for articles this way:
RewriteRule ^([^/\.]+)/([^/\.]+)/?article_(.*)_(.*).html$ index.php?sectionpp=$1&sectionpp2=$2&content=article&artid=$3&curbigart=$4 [L]
This produced the url like:
http://www.domain.com/Entertainment/114194/VINYETTE-TO-RELEASE-DEBUT-ALBUM-ON-MAY-7-2013
However, now I have redesigned the site and made the urls shorter:
RewriteRule ^([^/\.]+)/([^/\.]+)$ index.php?sectionpp=$1&content=article&artid=$2&curbigart=$3 [L]
Which produces url like:
http://www.domain.com/Entertainment/114194
The QUESTION:
Is it possible to redirect the already rewritten url
http://domain.com/Entertainment/114194/VINYETTE-TO-RELEASE-DEBUT-ALBUM-ON-MAY-7-2013
to
http://domain.com/Entertainment/114194
And obviously we're not talking about single page, but thousands, so good one line in .htaccess would be needed hehe. I know I could simply redirect in php, but I assume there must be a way to do that in .htaccess as well?
Thanks!
Is it possible to redirect the already rewritten url domain.com/Entertainment/114194/VINYETTE-TO-RELEASE-DEBUT-ALBUM-ON-MAY-7-2013
to domain.com/Entertainment/114194
You can use rule as very first rule in your root .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([\w-]+/\d+)/.+$ /$1 [L,R=302]

Htaccess, 301 redirect with exception

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]