rewrite old url to new one - .htaccess

I have this is current url pattern for a group of urls
http://www.example.com/assets/img/banner.jpg
which will be changing to
http://www.example.com/assets/img/all_banners/banner.jpg
How do I use mod_rewrite to rewrite these urls to the new pattern using .htaccess file?
I have this so far but it is not working...
RewriteRule /assets/img/(.*) /assets/img/all_banners/$1 [R=301,NC,L,QSA]
Maybe the flags are wrong?

You can use this rule as your very first rule:
RewriteEngine On
RewriteRule ^/?(assets/img)/([\w-]+\.[a-z]+)$ /$1/all_banners/$2 [R=301,NC,L,NE]
Make sure to clear your browser cache or use a new browser for testing.

Related

htaccess rewrite to new domain and url starting and ending with

My site's server is in the U.S. but I want to load images (and maybe later js and css) from a server in the visitors' country itself. I'm wondering what's a good way of rewriting only the images' urls that are in a specific directory.
Current url
http://www.myusserver.com/wp-content/uploads/image-name.jpg
url I want to use
http://www.myvisitorsserver.com/wp-content/uploads/image-name.jpg
I found this rewrite rule that does the job
RewriteRule ([^.]+\.(jpe?g|gif|bmp|png))$ http://www.myvisitorsserver.com/$1 [R=301,L,NC]
but I want to limit it to only images that are in the /wp-content/uploads directory.
I changed the rewrite to this
RewriteRule ^wp-content/uploads/ http://www.myvisitorsserver.com/wp-content/uploads/$1 [R=301,L,NC]
I think it's working, but I'm wondering if it's possible to rewrite only image urls. So, basically what I need is is to know how to rewrite and url starting with /wp-content/uploads and ending with an image extension.
I like this rule
RewriteRule ([^.]+\.(jpe?g|gif|bmp|png))$ http://www.myvisitorsserver.com/$1 [R=301,L,NC]
but I don't know how to change the first part of it to match /wp-content/uploads
Can anyone help me with this?
Thank you
I want to rewrite to the url structure as I mentioned in the 2nd example
Then just prefixing your rule pattern with the static path should work:
RewriteRule ^(wp-content/uploads/[^.]+\.(jpe?g|gif|bmp|png))$ http://www.myvisitorsserver.com/$1 [R=301,L,NC]

.htaccess rewrite conflicting rules

I'm having an issue with some htaccess rules which I thought would be simple. I have some nice SEO friendly URL rewriting in place as below
RewriteEngine On
RewriteCond %{REQUEST_URI} !^(index\.php|/images|/templates|/views|/ajax|/uploads|/robots\.txt|/sitemap\.xml|/favicon\.ico|/scripts|/cron|/combine.php|/js|/css)
RewriteRule ^(.*)$ index.php?ref=$1&%{QUERY_STRING} [L]
This all works well and I want to keep this. I also wish to rewrite some old pages which Google WMT is reporting as 404's to the new equivalent and for that I'd like to use:
Redirect 301 /about_us http://example.com/about-us
The problem I have is that the URL that the browser is directed to is:
http://example.com/about-us?ref=about_us
The about_us is the old link and about-us is the correct link. If the htaccess redirected to example.com/about-us then the other SEO friendly rewrite rule will pick it up and show the page but eh extra ?ref= parameter is confusing it. I am guessing the two rules are conflicting to a degree but is there a way to get the two rules to work together e.g. redirect without the extra ?ref= parameter? My knowledge of htaccess is basic to say the least so I am a little stuck on this one.
Thanks in advance
Redirect and RedirectMatch are part of mod_alias, while the rewrite rules are part of mod_rewrite. The problem you're running into is when you mix the two, both modules affect the same request, thus two things happen when you only want one. In this case, you need to stick with just mod_rewrite and use this instead:
RewriteEngine On
RewriteRule ^about_us /about-us [L,R=301]
RewriteCond %{REQUEST_URI} !^(index\.php|/images|/templates|/views|/ajax|/uploads|/robots\.txt|/sitemap\.xml|/favicon\.ico|/scripts|/cron|/combine.php|/js|/css)
RewriteRule ^(.*)$ index.php?ref=$1&%{QUERY_STRING} [L]
Note that the rule that redirects comes before the rule that routes to index.php.

prefix a friendly url using mod rewrite in htaccess

Is it possible to prefix a htaccess rewrite rule
for example can a variable be used as a prefix to a url
website.com/$variable-for-sale/
/cupcakes-for-sale/
/pies-for-sale/
/flans-for-sale/
The idea is to then use that variable to display all the cupcakes/pies/flans for sale
How would this be written as a rewrite rule? Is it even possible?
Thanks
The first rule will take care of redirecting your ugly URL to Friendly like one.
The second rule will internally redirect it back so the browser URL remains the friendly URL while service the content of your page.
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
# Redirect /?cake=anything to /anything-for-sale/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+\?cake=([^&\s]+) [NC]
RewriteRule ^ /%1-for-sale/? [R=302,L]
# Internally forward /anything-for-sale/ to /?cake=anything
RewriteRule ^([^-]+)-for-sale/?$ /?cake=$1 [NC,L]
Keep in mind I am using R=302 its always better to use 302 which means temporary redirect while testing a new rule before making it permanent as the permanent will cache the information to your browser. Once the rule is confirmed to be working as expected change R=302 to R=301.
To extract variable, you need to use regex parentheses in the correct pattern, then you can use $1 to fetch the group:
RewriteRule ^([^-]+)-for-sale/$ /target.php?variable=$1 [L]
The "target" part is the script you use to display the "variable". Since your question doesn't mention what that is, you have to figure it out.

Using mod_rewrite to mask a directory/file name in a URL

I've taken my site down for some prolonged maintenance and am using mod_rewrite to send all requests to a single page: www.mysite.com/temp/503.php
This is my .htaccess file which works fine.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/temp/503.php [NC]
RewriteRule .* /temp/503.php [R,L]
However, what I'd also like to be able to do is to hide /temp/503.php in the resulting URL from the visitor.
I know this is perhaps trivial and I'm sure fairly simple to achieve, but with my limited mod_rewrite skills I can't seem to get it to work.
Any help would be much appreciated.
Thanks.
Just get rid of the R flag in the rewrite rule, which tells the rule to redirect the request, thus changing the URL in the browser's location bar. So the rule would look like:
RewriteRule .* /temp/503.php [L]
which internally rewrites the requested URI instead of externally telling the browser that it's been moved to a new URL.

.htaccess file using 2 different rewrite rules

I've got a .htaccess file that has got a rewrite rule in it as follows which works fine:
RewriteRule ^solicitorsin([^/]+)/all/([0-9]+)$ /search/searchresults.php?county=$1&page=$2 [L]
What I'm looking to do is to keep using this for if the page variable is 2 or higher, but if it's 1 I want to 301 redirect to a separate url (the same site) say http://www.domain.com/solicitorsinCOUNTY/
The problem is that if I try doing this using a 301 redirect or a rewrite rule it still performs the above rewrite rule as well so I end up with http://www.domain.com/solicitorsinCOUNTY/?county=COUNTY&page=1
I haven't done much with .htaccess before so I'm not even sure if this is possible, can anyone help please? It would be much appreciated.
If you are using a rewrite rule, then put the rule for page=1 above the other rule and make sure you have the [L] flag.
Alternatively, you can use RewriteCond to prevent the rule from being run on specific URLS like this:
RewriteCond %{REQUEST_URI} !^solicitorsin([^/]+)/all/1$
RewriteRule ^solicitorsin([^/]+)/all/([0-9]+)$ /search/searchresults.php?county=$1&page=$2 [L]

Resources