How to 301 mod_rewrite many similar URLs at once? - .htaccess

I have many URLs like this:
https://www.url.com/user/shop/location/scoobydoo
They all begin the same, but the username at the end is different. I am trying to redirect 301 them all to:
https://www.url.com/profile-scoobydoo
This works if I use:
RewriteCond %{HTTP_HOST} www\.url\.com$
RewriteCond %{REQUEST_URI} ^\/user\/shop\/location\/scoobydoo$
RewriteRule .* https://www.url.com/profile-scoobydoo [R=301,L]
The problem is, I have several thousand users and while generating them all would work, it would completely flood the .htaccess file. Surely there must be an easier way to do this by username?
But how? Any help is greatly appreciated.

Try :
RewriteEngine on
RewriteRule ^user/shop/location/(.+)$ https://example.com/profile-$1 [L,R=301]
You can also use RedirectMatch directive
for your url redirection
RedirectMatch 301 ^/user/shop/location/(.+)$ https://example.com/profile-$1

Related

How can I redirect old URLs to new URLs using htaccess? [duplicate]

I'm trying to redirect a folder and all its sub files to a URL with a .htaccess file.
But
Redirect 301 /abc/cba/ http://www.aaa.com/
Will make /abc/cba/ddd/index.html redirect to http://www.aaa.com/ddd/index.html
What I want is redirect /abc/cba/ /abc/cba/ddd/index.html to http://www.aaa.com/
Could anyone help? Thanks. If anything not clear, please let me know.
By default, Redirect sort of maps the path node to a new path node, so anything after the first path gets appended to the target URL.
Try:
RedirectMatch 301 ^/abc/cba/ http://www.aaa.com/?
Or if you'd rather use mod_rewrite instead of mod_alias:
RewriteEngine On
RewriteRule ^/?abc/cba/ http://www.aaa.com/? [R=301,L]
here's another example of a mod_rewrite rule that worked for me
I wanted to redirect a sub directory to the root of the same domain.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^sub_directory/(.*)$ /$1 [R=301,NC,L]
</IfModule>
more examples can be found here:http://coolestguidesontheplanet.com/redirecting-a-web-folder-directory-to-another-in-htaccess/
I perfer the following method:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/somedir [NC]
RewriteRule /(.*) http://somesite.com/lost/$1 [R=301,L]
I had to reroute urls from old site version to new version, so here is what I did to reroute any links from about-us/* to about-us.html
RewriteEngine on
RewriteRule ^about-us/(.*)$ about-us.html [R=301,L]
What it doesn't do is rewrite something like domain.com/about-us/thing.html => domain.com/about-us.html .
It does work for things without extensions domain.com/about-us/something-in-url => domain.com/about-us.html
I added the lines below to redirect .jpg and .png, but it didn't work for .html, I can't find out why.
RewriteRule ^about-us/(.*).jpg about-us.html [R=301,L]
RewriteRule ^about-us/(.*).png about-us.html [R=301,L]

.htaccess 301 redirect rule

can someone tell me how to write rewrite rule: I have many links that look like:
http://www.mavrica.com/index.php?eID=tx_cms_showpic&file=uploads%252Fpics%252Fmozic_05.jpg&width=800m&height=600m&bodyTag=%253Cbody%2520bgcolor%253D%2522black%2522%253E&wrap=%253Ca%2520href%253D%2522javascript%253Aclose()%253B%2522%253E%2520%257C%2520%253C%252Fa%253E&md5=025892981ebd7f312b96276beb3ee194
I would like to redirect all of them to http://www.mavrica.com/fotogalerije/
All the links have in common first part (up to tx_cms_showpic).
I tried the following htaccess rules:
RewriteRule /index.php?eID=tx_cms_showpic$ http://mavrica.com/fotogalerije/ [R=301]
and with
RedirectMatch 301 ^/index.php?eID=tx_cms_showpic(.*) http://www.mavrica.com/fotogalerije/
but none of them work.
What have I missed out?
Thanks for help!
You need to use %{QUERY_STRING} to capture and/or match the query string part of the URL:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?eID=tx_cms_showpic [NC]
RewriteRule ^ http://mavrica.com/fotogalerije/? [R=301,L]

How can I redirect old pages with question marks in the URL?

I have read about htaccess redirect and rewrite on stackoverflow and on other sites and learned how to redirect simple pages and directories, but there are about 30 links remaining that I haven't been able to redirect. The reason appears to be because they contain "?" in the link's URL. I've tried the solutions posted but I haven't been able to make enough sense of them to succeed.
These work:
Redirect /Corpfleet.php htp://www.marketyourcar.cm/wraps.php
Redirect /drivers.php htp://www.marketyourcar.cm/drivers.php
Redirect /galleries.php htp://www.marketyourcar.cm/galleries.php
These do NOT work:
Redirect /ad.php?View=FAQ htp://www.marketyourcar.cm/advertiser-faqs.php
Redirect /ad.php?View=gallery htp://www.marketyourcar.cm/galleries.php
Redirect /ad.php?View=Materials htp://www.marketyourcar.cm/products-services.php
Yes, I know that the URL above is htp and .cm - I had to break it in order to make this post with my low reputation level.
If anyone can help with this I'd appreciate it.
Thanks
If you want to redirect from like:
site.com/index.php?blabla=1&id=32
to
site.com/contact.html
then use:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{QUERY_STRING} blabla=1&id=32
RewriteRule ^(.*)$ http://www.site.com/contact.html? [R=301,L]
</IfModule>
Redirect can't handle that. RewriteRule can. This should work.
RewriteEngine on
RewriteRule ^/ad\.php\?View\=FAQ$ http://www.marketyourcar.cm/advertiser-faqs.php [R=301,L]
RewriteRule ^/ad\.php\?View\=gallery$ http://www.marketyourcar.cm/galleries.php [R=301,L]
RewriteRule ^/ad\.php\?View\=Materials$ http://www.marketyourcar.cm/products-services.php [R=301,L]
Or try this:
RewriteEngine on
RewriteRule ^/ad.php?View=FAQ$ http://www.marketyourcar.cm/advertiser-faqs.php [R=301,L]
RewriteRule ^/ad.php?View=gallery$ http://www.marketyourcar.cm/galleries.php [R=301,L]
RewriteRule ^/ad.php?View=Materials$ http://www.marketyourcar.cm/products-services.php [R=301,L]
This might work:
Example:
RewriteEngine On
RewriteRule ^/ad.php?View=FAQ$ http://www.marketyourcar.cm/advertiser-faqs.php? [R=301,L]
Add a trailing ? to the substitution URL to remove the incoming query.

htaccess 301 redirect folder, but not subfolders

I need to redirect from:
http://example.com/folder
to http://example.com/newfolder
But leave:
http://example.com/folder/subfolder
Where it is. Is this possible? I can't seem to make it happen without causing a heap of redirect chaos.
Jestep's answer above redirects "/root-directory" but fails to redirect "/root-directory/". This can be fixed and simplified by simply:
RewriteCond %{REQUEST_URI} ^/folder/?$
RewriteRule (.*) /newfolder [R=301,L]
This will redirect "/folder" and "/folder/" but leave all sub-directories alone.
I've tried other methods here with mixed success. I am no Apache expert by any means, but here is the simple one-liner that works every time for me. I just use RedirectMatch instead of Redirect and include a very simple RegEx to end the match at or just before the trailing slash, meaning that subdirectories should never qualify as a match.
RedirectMatch 301 ^/folder[/]?$ /newfolder
I just ran into this and here's the solution I came up with. I prefer this method because it doesn't redirect any subdirectory.
RewriteCond %{REQUEST_URI} ^/root-directory[/]?
RewriteCond %{REQUEST_URI} !^/root-directory/+[/]?
RewriteRule (.*) http://www.example.com/ [R=301,L]
Maybe:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/folder[/]?
RewriteCond %{REQUEST_URI} !^/folder/subfolder[/]?
RewriteRule (.*) /newfolder/$1 [R=301,L]
This should redirect /folder to /newfolder but leave out /folder/subfolder
If mod-rewrite isn't enabled or you are unable to use RewriteRule directive on your server, you can use RedirectMatch directive of mod-alias which is the default module of apache httpd server.
RedirectMatch 301 ^/folder/?$ http://example.com/newfolder/
This will 301 redirect /folder/ to /newfolder/ .
Which server are you using?
You could for example use mod_rewrite if you use apache and do something like this
RewriteEngine On
RewriteOptions Inherit
RedirectMatch permanent ^/folder/$ http://example.com/newfolder
#I haven't tested the above redirect btw ^
and put that in a .htaccess file in your /folder/ directory (assuming you can alter apache's settings, meaning you have the option AllowOverride All in that virtual host)
Here's some more info http://httpd.apache.org/docs/2.0/misc/rewriteguide.html

htaccess 301 redirect rule

I want to
redirect http://www.mysite.com/index.php?option=com_content&view=frontpage&Itemid=1
TO
http://www.mysite.com/
Can you please show me the 301 redirect rule for htaccess?
Thanks.
I tried the following, but no luck.
RewriteCond %{QUERY_STRING} ^option=com_content&view=frontpage&Itemid=1$
RewriteRule ^/index.php$ http://www.mysite.com [L,R=301]
You can try below configuration,
RewriteCond %{QUERY_STRING} option=com_content&view=frontpage&Itemid=1
RewriteRule index\.php$ /? [L,R=301]
I tried it on my domain and it works fine. Hope this works for you too... :)
According to www.htaccessredirect.net, the code to do this would be:
Redirect 301 /index.php?option=com_content&view=frontpage&Itemid=1 /
Alternatively, you could look in to using apache's mod_rewrite module.

Resources