URL Redirection by removing a string - .htaccess

I have to redirect some url .
for example if the url is
www.example.com/shop valid
www.example.com/shop/red-product need redirection to www.example.com/red-product
www.example.com/shop/green need redirection to www.example.com/green
www.example.com/shop/any-string need redirection to www.example.com/any-string
How i can do this . Please help .
My current htacess file is
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Redirect 301 /bio/ https://www.example.com/bio/
Options -Indexes

You may replace your code with this one:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteRule ^shop/(.+)$ /$1 [L,NC,NE,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

Below rule should work we are matching the group after shop only.
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/shop/(.*)
RewriteRule ^ http://www.example.com/%1 [R]

Related

www + http To https forward

I want to forward request like this
http://www.website.com/ > https://www.website.com/ > https://website.com/
My .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Try :
RewriteCond %{HTTP_HOST} !^website\.com$
RewriteRule ^ https://domain\.com [R=301,L]
Based on your shown samples, try. Please make sure you clear your browser cache before testing your URLs. Make sure you are placing your https rule at the top of your htaccess file.
<IfModule mod_rewrite.c>
RewriteEngine ON
RewriteBase /
RewriteCond %{HTTP_HOST} ^(?:www\.)(.*)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [NE,R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

Redirecting referrer URLs based on new site structure

So I'm trying to redirect incoming connections based on the referrer URL. So for instance if someone was trying to visit:
blog.example.com/category/fancy-stuff
They are already getting redirected to example.com but I want to redirect them to:
example.com/news/category/fancy-stuff
Right now I have this for my htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteCond %{HTTP_REFERRER} blog.example.com/category/* [NC]
RewriteRule ^ example.com/news/category/*? [R=301,L]
</IfModule>
Is there a way to use variables or something to just append the proper category to the new URL?
Reorder your Rewrite rules :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_REFERRER} ^blog.example.com/category/.*$ [NC]
RewriteRule ^(.*)$ http://example.com/news/$1 [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

Change URL with rewrite rule

How can I change my URL from domain.com/sprekers/?spreker=value to domain.com/value with a rewrite rule with wordpress.
I've tried to change it like this
RewriteCond %{QUERY_STRING} ^spreker=(.*)$
RewriteRule ^sprekers/$ %1/? [R=301,L]
than I tested it with the htaccess tester http://htaccess.madewithlove.be If I test it on the tester everything works fine but when I do this on my wordpress site it doesn't work. This is my complete htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteCond %{QUERY_STRING} ^spreker=(.*)$
RewriteRule ^sprekers/$ %1/? [R=301,L]
</IfModule>
I think it doesn't work because the /sprekers is already "made" by wordpress how can I fix this?
Cheers
Robin
I think, you need to put your first code after the RewriteBase / rule.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^spreker=(.*)$
RewriteRule ^sprekers/$ %1/? [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

how rewrite url from subfolder at root

I have more link in Google es:
http://www.mysite.com/blog/23-08-2012/example.html
http://www.mysite.com/blog/more/
http://www.mysite.com/blog/test/example.html
how to rewrite the url by removing the word "blog" in htaccess es:
http://www.mysite.com/23-08-2012/example.html
http://www.mysite.com/more/
http://www.mysite.com/test/example.html
EDIT
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
RE-EDIT solution
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
RedirectMatch 301 ^/blog/(.*)$ /$1
it is important to put this line at the end, not the beginning
RedirectMatch 301 ^/blog/(.*)$ /$1
You only need mod_alias to do this:
RedirectMatch 301 ^/blog/(.*)$ /$1
You can use that in your vhost config or the htaccess file in your document root.
Surprise that you don't have mod_alias, it's pretty much loaded on default. But since you're already using mod_rewrite:
If this is what you have:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Then above the RewriteBase, add this:
RewriteRule ^blog/(.*)$ /$1 [L,R=301]
Just a minute or two to read Apache's Rewrite Rule documentation would've provided you with answer. Here's an outline to what you can do:
Options +FollowSymLinks
RewriteEngine On
RewriteCond ^blog/
RewriteRule ^blog/(.*)$ http://mysite.com/$1 [R=301,L]

rewrite/redirect specific word from url .htaccess

I have couple of urls which is generated by wordpress/php
http://mydomain.com/news-2/newsarticle
http://mydomain.com/products/category-2
http://mydomain.com/products/category/products-2
how can I rewrite/redirect any url with -2 to the one without? The result should be
http://mydomain.com/news/newsarticle
http://mydomain.com/products/category
http://mydomain.com/products/category/products
This is what I have so far
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Thanks
Add this to you htaccess :
RewriteRule ^(.*)-2(.*)$ /$1$2 [NC,L,R=301]

Resources