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]
Related
I am changing my website from a dynamic CMS-system (Umbraco) to a static classic .HTML. It is the on the same domain, but the URL will change.
Example: The URL is changing from:
www.example.com/information
To:
www.example.com/info.html
My question is:
What is the best way to redirect while keeping the best SEO page rank.
I am thinking about 301 redirect through .htaccess, but I am not sure if I should redirect my new to .html urls to the old dynamic …/example - or the other way?
Or maybe there is a different better way?
I do have a fine 404.
Also I need the right redirect code for .htaccess - if that's the right way.
I hope you guys can help me out.
I haven't try anything out yet, because I don't wanna do 301 before the site go live.
You need to implement 301 redirects from the old URL to the new URL in order to preserve SEO and ensure that any "old" links that have been bookmarked or linked to from other websites still work.
Exactly how you implement the 301 redirect (either in your server-side script or in .htaccess) does not really matter. However, if you are moving to an entirely static site then .htaccess is likely the only option you have.
I am not sure if I should redirect my new to .html urls to the old dynamic …/example - or the other way?
You need to redirect from the "old" URLs to the "new" URLs that you are using/linking to on the new site. (It makes no sense to redirect the other way as that would just break everything!)
You can probably just use the simple mod_alias Redirect directive.
For example, to 301 redirect from /information to /info.html you could do the following:
Redirect 301 /information /info.html
Bear in mind that 301 redirects are cached persistently by the browser. To prevent caching issues it is advisable to test first with a 302 (temporary) redirect.
Have you considered keeping the same URLs? This would obviously negate the need for implementing redirects. You could employ URL-rewriting if the underlying file is called info.html. For example, using mod_rewrite:
RewriteEngine On
RewriteRule ^information$ info.html [L]
The above would internally rewrite a request for /information to info.html. The user only sees /information in the browser address bar, but info.html is served from your site.
Taking this further, it would be easier if the new "file" is simply the same as the old URL, just with a .html extension. For example, the URL is /information and the underlying file is information.html. You can then use a single rule to rewrite all your URLs. For example:
RewriteCond %{DOCUMENT_ROOT}/$1.html -f
RewriteRule ^([^/.]+)$ $1.html [L]
The above assumes the old URLs do not contain additional slashes (ie. consist of a single path segment. In other words, all files are in the document root) and do not contain dots.
White
I finally got my page ready to go live, and i changed all my new URLS to the same name as the old URL, just with a .html extension - as u said. After that i used:
RewriteCond %{DOCUMENT_ROOT}/$1.html -f
RewriteRule ^([^/.]+)$ $1.html [L]
And it works fine.
I do have a question about if a "RewriteRule ^(.*).html$ /$1 [L,R=301]" would be better? I mean both "/page.html" and "/page/" works, and this could mess with my former SEO ranking?
Also: what do u think of this:
RewriteCond %{THE_REQUEST} /([^.]+)\.html [NC]
RewriteRule ^ /%1 [NC,L,R]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^ %{REQUEST_URI}.html [NC,L]
RewriteRule ^(.*)\.html$ /$1 [L,R=301]
And by the way. Do you use "page.html" or just "/page" in your href to prevent the .html showing?
//MM
For dynamic content on my website, I use urls with query variables
e.g. http://www.example.com/directory/?state=NJ&city=Wayne&zip=07474 or
http://www.example.com/directory/index.php?state=NJ&city=Wayne&zip=07474
I would like to convert them to seo-friendly static urls instead
e.g. http://www.example.com/directory/Wayne-NJ-07474/
I researched on SO and found it seems like a htaccess rewrite rule is the way to go.
I am a noob when it comes to htaccess constructs and what I fail to understand is will I still get the data associated with the individual query parameters i.e. state, city, and zip. Meaning will I still be able to access them via _REQUEST()?
I would also appreciate if someone would help me with the rewrite rule for my specific case.
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+directory/(?:index\.php)\?state=([^\s&]+)&city=([^\s&]+)&zip=([^\s&]+)\s [NC]
RewriteRule ^ /directory/%2-%1-%3? [R=302,L,NE]
# internal forward from pretty URL to actual one
RewriteRule ^directory/([^-/]+)-([^-/]+)-([^-/]+)/?$ directory/?state=$2&city=$1&zip=$3 [L,QSA,NC]
I am really stuck with my .htaccess file, and need some help :). I have a WordPress installation that I am using for testing. It is in a folder and I use .htaccess to get there. This is the rules I use so far:
######### Custom #########
RewriteEngine On
# ignore folders
RewriteCond %{REQUEST_URI} "/af1wp/"
RewriteRule (.*) $1 [L]
###############
# only for me #
###############
# HOME (Senne Tijdeman)
RewriteCond %{REMOTE_ADDR} ^###\.###\.###\.###$
RewriteCond %{HTTP_HOST} ^((www.)?([a-z0-9_\-]+).)?alleenf1.nl$
RewriteCond %{REQUEST_URI} !^/af1wp/$
RewriteRule ^(.*)$ /af1wp/$1 [L]
This works (with my real IP address of course), so no problem there. But now I want to rewrite exisiting URL's to a new format. The old URL is this:
http://alleenf1.nl/nieuws/QOgbb/raikkonen-alles-is-mogelijk-in-australi
The new URL should be this:
http://alleenf1.nl/raikkonen-alles-is-mogelijk-in-australi
The part I want to remove "nieuws/QOgbb/" is not always the same, so I have to use regex for that. But everything I tried did not work at all.
I thought this would be simple, but apparently not for me unfortunately. Now I have 2 questions.
What is the right RewriteRule to do this?
Where should I put it. In the .htaccess of the root folder, or the af1wp folder where the WordPress install is?
Tnx in advanced
To awnser the questions from poncha below:
Yes, the URL's always start with to folders. Just to clarify (was not clear) the part "nieuws" is always the same, but not the second part (call it an ID).
I prefer a redirect.
The file /raikkonen-alles-is-mogelijk-in-australi is a post in WordPress. That WordPress installation currently resides in the folder af1wp, but will be moved to the root folder when going live.
Try this:
RewriteEngine On
RewriteRule ^nieuws/([^/]+)/(.*) /af1wp/$1 [R=301,L,QSA]
This will only match URLs starting with "nieuws"
For now, the rewrite target is /af1wp/, change it to / when moving the wordpress.
When you move wordpress, you'll need to mix in this rule inside the wordpress rules, as it already has rewrite rules of its own - place this rule above its rules.
The flags used here:
R=301 - redirect with HTTP status 301 (Moved Permanently).
L - last rule (stop rules parsing after successful match of this rule)
QSA - query-string-append (append original query string to the rewritten request).
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.
I am working on wordpress blog and the its has the url pattern something like this. localhost/blog/2012/10/12/sample-post/ .
Now i have updated the url pattern to localhost/blog/sample-post/.
Now i need a redirection to second URL pattern if someone enters the first URL Pattern as i have already submitted URLs with first pattern to couple of articles.
i.e, localhost/blog/2012/10/12/sample-post/ should be redirected to localhost/blog/sample-post/
Can anyone assist me by providing htacess code ?
Try adding these to the htaccess file in your document root. Make sure they are before any wordpress related rules:
RewriteEngine On
RewriteRule ^/?blog/[0-9]{4}/[0-9]{2}/[0-9]{2}/(.*)$ /blog/$1 [L,R=301]