Exclude image urls from redirection using htaccess - apache

My old blog was at www.example.com/myblog
I changed my server and I want to use www.example.com/blog
To do this I used htaccess code
Redirect 301 /myblog http://www.example.com/blog
This helped in redirecting the blog home page and individual blog posts.
The images were put in a folder named myblog in the new server.
So the image url is www.example.com/myblog/wp-content/uploads/2010/04/something.jpg
But the htaccess I wrote above redirects this to www.example.com/blog/wp-content/uploads/2010/04/something.jpg which returns 404.
Can anyone help me to write htaccess code for redirecting blog post urls only excluding the blog image urls?

Instead of using mod_alias and the Redirect directive, try mod_rewrite and add some conditions:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?myblog/(.*)$ http://www.example.com/blog/$1 [L,R=301]
# redriect the index
RewriteRule ^/?myblog/?$ http://www.example.com/blog/ [L,R=301]

Related

Prerender.io not working when redirecting root path with apache mod rewrite

I am trying to redirect my root path www.example.com to www.example.com/en/
But when I use this the root path for prerender redirects too.
So for example when I go to this www.example.com/?_escaped_fragment_= it redirects directly to www.example.com/en/ instead of www.example.com/en/?_escaped_fragment_=
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} example\.com [NC]
RewriteCond %{REQUEST_URI} ^/$
Rewriterule ^(.*)$ http://www.example.com/en/ [L,R=301]
</IfModule>
Please help thank you.
That is expected and working correctly.
If a user goes to www.example.com/ then they should get redirected to www.example.com/en/.
So if a crawler goes to www.example.com/?_escaped_fragment_= then they should also get redirected to www.example.com/en/.
The crawler will update their index and they will eventually crawl www.example.com/en. If they find the meta fragment tag then they will crawl www.example.com/en/?_escaped_fragment_=.
So that's just all part of the process of the escaped fragment crawling scheme.

Htaccess 301 redirect issue, previous link remains in URL bar with tag attached

Edit Yes, It is a stupid question, but I can't find a solution in enough time that pertains to the problem.
I'm trying to create some redirect links since I updated my website, as the old ones are no longer in use. I have my .htaccess file set up as follows:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*) index.php?url=$1 [QSA,L]
# Permanent URL redirect - generated by www.rapidtables.com
Redirect 301 /posts/content/2/building-your-own-mvc-framework-in-php-part-1 https://ciangallagher.net/#post2
Which works, however it leaves a ?url= tag in the URL bar with the original redirected link as so:
How can I amend my .htaccess file to remove this tag and just reidrect to the desired link which would be https://ciangallagher.net/post2?
I resolved this, the issue was that the RewriteRule I had already set was interfering with the redirects.

.htaccess in subfolder doesn't work

I'm hosting my site on Godaddy server.
On the server I have 5 sites, each site on individual folder.
for example: "webroot/site1"
I directed specific domain to the specific folder on the server "/site1"
The problem:
I tried to make a RewriteRule with .htaccess in the subfolder but it keeps telling me 404 not found.
This is the .htaccess:
RewriteEngine On
this works fine:
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301]
this redirect to 404 page:
#rewirite categories only
RewriteRule ^category/([0-9]+)/.*$ /category.php?c_id=$1 [L,QSA]
this url works perfect:
www.example.com/category.php?id=1
this url doesn't work and redirect to 404 page
www.example.com/category/1/blalba
More details:
The other 4 folders are word press sites.
In the webroot there is another wordpress site.
In the webroot there is an empty htaccess file.
I already tried to do
I already tried to do:
RewriteRule ^category/([0-9]+)/.*$ /site1/category.php?c_id=$1 [L,QSA]
EDIT:
I don't have category folder is just for the URL I want that it will redirect me to category.php with the id parameter
This is the error I get:
Not Found
The requested URL /site1/category/1/area-rugs was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
The RewriteRule will need to be in the htaccess file in the root folder of the website.
E.g. if the directory structure is something like this:
/webroot/site1.com/
/webroot/site1.com/.htaccess
/webroot/site1.com/index.php
/webroot/site1.com/category.php
I'd have this code in the .htaccess file in site1.com:
RewriteEngine On
# Force www
RewriteCond %{HTTP_HOST} ^site1.com
RewriteRule ^(.*)$ http://www.site1.com/$1 [R=301]
# Handle categories
RewriteRule ^category/([0-9]+) /category.php?c_id=$1 [L,QSA]

Apache .htaccess: Serving .css files from separate domain?

I'm trying to serve CSS files for my site from a separate domain.
I've got the following entry in my site's .htaccess (in the document root):
RewriteRule ^templates/*\.css$ http://css.mysite.com/templates/$1 [R=301,L]
With the intention of simply matching:
http://www.mysite.com/templates/default/styles/main.css
… and sending an HTTP 301 redirect to:
http://css.mysite.com/templates/default/styles/main.css
But what's actually getting called is:
http://css.mysite.com/templates/.css
(I'd like to duplicate the above for *.js and serve them from js.mysite.com and all png & jpgs and serve them from img.mysite.com.)
Any insights would be fantastic. I've got to admit, htaccess rewrite rules always seem to elude me.
Try this and let me know:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^css\.
RewriteRule ^templates/(.*)\.css$ http://css.mysite.com/templates/$1.css [R=301,L]
or a more "dynamic way"
RewriteCond %{HTTP_HOST} ^(www\.)?mysite
RewriteRule ^templates/(.*)\.(js|css)$ http://$2.mysite.com/templates/$1.$2 [R=301,L]
RewriteRule ^templates/(.*)\.(jpg|gif|swf|jpeg|png)$ http://imgs.mysite.com/templates/$1.$2 [R=301,L]
This one will check for JS and CSS and use the subdomain based on the file extension: so .js will go to js.mysite.com and .css will go to css.mysite.com and same for images but these goes to imgs.mysite.com instead.
or be even more generic:
RewriteCond %{HTTP_HOST} ^(www\.)?mysite
RewriteRule ^templates/(.*)$ http://media.mysite.com/templates/$1 [R=301,L]
You redirecting everything that start with templates

Htaccess 301 only part of the redirect works

Hi Im moving a site from one domain to another, and I have created the following .htaccess file, but its not working.
*#Options +FollowSymlinks
RewriteEngine On
redirect 301 http://www.el-netshop.dk/pi/Dækkape_UG150_12_lysegrå_5302_.aspx http://www.el-netsalg.dk/pi/Dækkape_UG150_12_lysegrå_5271_.aspx
RewriteCond %{HTTP_HOST} ^el-netshop.dk$ [OR]
RewriteCond %{HTTP_HOST} ^www.el-netshop.dk$
RewriteRule (.)$ http://www.el-netsalg.dk/$1 [R=301,L]
I would like it to work like this.
Have a list of urls where the url is diffent, with more then just the domain. Ex. in the above the from link contains 5302 but to link is 5271.
Then with the rest, I want it to make a normal redirect.
The above code just do (.*)$ http://www.el-netsalg.dk/$1 and ignores the special cases.
What am I doing wrong?
According to the apache docu the syntax is as folows:
Redirect 301 /service http://foo2.bar.com/service
So try:
Redirect 301 /pi/Dækkape_UG150_12_lysegrå_5302_.aspx http://www.el-netsalg.dk/pi/Dækkape_UG150_12_lysegrå_5271_.aspx
without the "http://www.el-netshop.dk" for the old-path paramater.