Rewrite url for subdirectory in .htaccess - regex

I moved wordpress files from root to a subdirectory. There are some files uploaded in wp-content/uploads folder. When accessing those files directly, I get a 404 error.
I need to rewrite the url only for the wp-content/uploads through .htaccess
What I need is to redirect from
http://example.com/wp-content/uploads/2012/09/report.pdf
to
http://example.com/wp_sub/wp-content/uploads/2012/09/report.pdf
This is what I've tried
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteRule ^(.*)$ http://www.example.com/wp_sub/$1 [R=301,QSA,L]
This adds a subdirectory wp_sub to every link.
I need to add a subdirectory "wp_sub" only for wp-content/uploads links.
What am I missing?

Let's see what
RewriteRule ^(.*)$ http://www.example.com/wp_sub/$1
means:
You rewrite everything (^(.*)$) to http://www.example.com/wp_sub/$1.
Well, you don't want to rewrite everything, do you? :)
So we have to restrict this to only affect the wp-content folder:
RewriteRule ^(wp-content/.*)$ http://www.example.com/wp_sub/$1 [R=301,QSA,L]

Place this rule in your /wp-content/uploads/.htaccess file:
RewriteEngine On
RewriteBase /wp-content/uploads/
RewriteRule ^(.*)$ /wp_sub/$1 [L,R]

You can use the code :
RewriteRule ^wp-content/uploads/(.*)$ http://www.example.com/wp_sub/wp-content/uploads/$1 [R=301,L]

Related

Redirect non-www to www giving me hundreds of 404's

Thanks to anyone who can take a moment to look at this.
Recently I created a new section "subdomain" in my website and in this new folder I have includes a Joomla CMS installation the url looks like this: http://www.example.com/subdomain/
In this folder I have a htaccess file to which I have added.
## No directory listings
# Redirect non-www to www:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
When I try to access say http://example.com/subdomain/anytrailingstring then it's NOT redirecting me to http://www.example.com/subdomain/anytrailingstring as I expected, it is redirecting to http://www.example.com/anytrailingstring leaving out the /subdomain/ and this is of course a page that doesnt exist and therefore a 404.
This is a problem.
I do not have any directive in the root .htacces file except for this :
DirectoryIndex index.php
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Can someone perhaps see why the subdomain htaccess isnt redirecting to correctly? Did I miss something?
I am not good with htaccess at all, if anybody can help me I would really appreciate it.
Thanks!
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
You need to use the REQUEST_URI server variable instead of the backreference ($1). The URL-path matched by the RewriteRule pattern (first argument) is relative to the current directory, so excludes the parent subdirectory (ie. /subdomain in your example).
Do it like this instead:
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
You will need to clear your browser cache since the erroneous (301 - permanent) redirect will have been cached by the browser. Test with 302 (temporary) redirects to avoid potential caching issues.
However, a couple of questions:
Why are you not using HTTPS? (You are redirecting to HTTPS in the parent .htaccess file - but this is now being overridden by the mod_rewrite directives in the subdirectory.)
Why not include this in the parent .htaccess file?
UPDATE: So, taking the above points into consideration... if you want to move this rule to the parent .htaccess file in the root then have it like this:
DirectoryIndex index.php
Options +FollowSymLinks
RewriteEngine on
# Redirect non-www to www (and HTTPS)
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Redirect HTTP to HTTPS
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
The order of the directives is to ensure there is only ever at most 1 redirect (assuming you are not implementing HSTS).
You were unnecessarily duplicating the RewriteEngine directive (so I removed the second instance).
The RewriteBase directive was not being used.
The capturing subgroup in your HTTP to HTTPS rule was not required. ie. ^ is better than ^(.*)$ in this instance.
Aside:.
...a new section "subdomain" in my website and in this new folder I have includes a Joomla CMS installation the url looks like this: http://www.example.com/subdomain/
This is a subdirectory, not a "subdomain".
This is a "subdomain":
http://subdomain.example.com/

improvement of a mod_rewrite rule

Could you have a look at the code below and say if this is the correct way to do it please.
In order to redirect from:
http://blog.exampledomain.com/2013/10/testpage.html
to
https: //exampledomain.com/blog/testpage/
I have placed the below rewrite rules in 2 .htaccess files:
In the .htaccess file in root of exampledomain.com I have placed:
RewriteCond %{HTTP_HOST} ^blog\.exampledomain\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.blog\.exampledomain\.com$ [NC]
RewriteRule ^(.*)$ https://exampledomain.com/blog/$1 [R=301,L]
In the .htaccess file in the blog folder in exampledomain.com/blog I have placed:
RewriteRule ^([0-9]{4})/([0-9]{2})/(.*)\.html https://exampledomain.com/blog/$3/ [R=301,L]
Questions:
Is this the correct way to do it?
Does this mean in this setup we have 2 redirects? (which is probably not good for SEO) Should these be combined? How?
These two redirects should be combined into one.
In the .htaccess file in the document root of your subdomain (which looks it might be the same as the document root of the main domain? Or in the main domains .htaccess file - if this is a parent directory on the filesystem), try the following:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?blog\.exampledomain\.com$
RewriteRule ^\d{4}/\d\d/(.+)\.html$ https://exampledomain.com/blog/$1/ [R,L]
When you are sure this is working OK then change the temporary redirect (R) into a permanent one (R=301).
UPDATE: To also redirect from: http://blog.exampledomain.com/ to https://exampledomain.com/blog/ and http://blog.exampledomain.com/whatever to https://exampledomain.com/blog/whatever, add the following directives after the above:
RewriteCond %{HTTP_HOST} ^(www\.)?blog\.exampledomain\.com$
RewriteRule ^(.*)$ https://exampledomain.com/blog/$1 [R,L]

htaccess URL Rewrite - Root to Subfolder (with folder of the same name)

I have a file at www.domain.com/blog/blog.html that I want to rewrite as www.domain.com/blog.
I have figured out the rewrite for removing the extension .html but am having either circular issues or end up pointing the blog rewrite to the blog folder when trying to do the above.
I am new to rewrite syntax, so help would be much appreciated!
Create /blog/.htaccess if it doesn't already exist and place this rule:
RewriteEngine On
RewriteBase /blog/
RewriteRule ^/?$ blog.html [L]
# To internally forward /blog/file to /blog/file.html
RewriteCond %{DOCUMENT_ROOT}/blog/$1\.html -f [NC]
RewriteRule ^(.+?)/?$ $1.html [L]

Remove last Path from URL using .htaccess

I need to create a RewriteRule on my .htaccess file which removes the Path ("page-2") if it's included at the end of the URL. So for example:
http://myhost.com/path/page-2
should redirect to:
http://myhost.com/path
I've found a similar solution on SO:
RewriteEngine On
RewriteRule ^([^/.]+)/?$ /$1/page-2/ [L]
However it does not work for me. No redirection happens.
Any help?
You need to have this rule:
RewriteEngine On
RewriteRule ^(.+?)/page-2/?$ /$1 [L,NC,R=301]

mod_rewrite with nested .htaccess files

I have made an .htaccess to my root directory for creating a subdomain level, assume it is sub.domain.ex that redirect to domain.ex/deb/
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} sub.domain.ex
RewriteCond %{REQUEST_URI} !deb/
RewriteRule ^(.*)$ /deb/$1 [L]
and this works well.
Now, I go to /deb/ and create another .htaccess with the following
RewriteEngine on
RewriteRule ^([^/]+)/ /deb.php?app=$1 [NC]
the deb.php is a file that prints the argument "app".
It works but only if i call http://sub.domain.ex/something/ (note the slash at the end). It only works with a final slash, if I remove it, it doesn't and I want it to work without final slash.
So I change the rule into ^([^/]+) but now I have 500 Apache internal error.
The regex coaches are by my side with the selection, maybe I'm missing something.
Thanks
UPDATE
I'm runinng mad. Mybe is wrong to put one .htaccess in the root for creating se 3th sublevel doman and the .htacces in the other directory? Because I'm ttrying some trick.
I use this rule
RewriteEngine on
RewriteRule ^([^/]+)/ deb.php?app=$1 [NC]
in the .htacces of the /deb directory and made a print_r($_GET); and called index.php. So the redirectory doesn't work at all if it forward me on the index.php of the cydia sublvel and doesn't take the /deb/deb.php!!!
Recap.
my dir structure is this:
/htdocs/ -> the root of the main domain level like www.example.com
---index.php -> home file of www.example.com
/htdocs/deb -> the root directory of the 3th sublevel domain (subdomain.example.com ->
---index.php
---deb.php
So the .htaccess for the 3th level domain is the in /htdocs./htaccess and described as before.
The other .htaccess for "beautify" the link is in the /htdocs/deb/.htaccess. I want that when you go to subdomain.domain.com/someText it transform to deb.php?app=someText
Now i tryed go to subdomain.domain.com/deb.php....WTF!? the deb.php is in /htdocs/deb/deb.php
Home is clear
This works fine for me, at least, assuming I understood everything you wanted.
In /htdocs/.htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} =sub.domain.ex
RewriteCond %{REQUEST_URI} !^deb/
RewriteRule ^(.*)$ /deb/$1
In /htdocs/deb/.htaccess:
RewriteEngine On
RewriteBase /deb/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ deb.php?app=$1
RewriteRule !^deb.php index.php
Edit: I updated the second file's contents to reflect your additional requests. You should remove the /? if you don't want them to be able to go to sub.domain.ex/something/ -> deb.php?app=something.
I could be wrong, but in the second .htaccess you should check that the url wasn't yet rewrited with "/deb.php"
So something like
RewriteCond %{REQUEST_URI} !deb\.php
RewriteRule ^([^/]+) /deb.php?app=$1 [NC]
You rewrite rule should be
RewriteRule ^(.*)$ deb.php?app=$1 [L,QSA]
That's a common pattern used in CMSs like Drupal
I think there is some kind of rescrctions to my host. It's impossible. I tried all kind of goo (for a regexcoach) for matching the correct group and there is nothing else the 500 error apache.
The only way to get this to work is use tu parameters
with this ^app/([^/]+) /deb.php?app=$1 and calling with sub.domain.com/app/nameTest and this is working with or without the end backslash.
Of you get some advice for getting rid of this, please let me know.
bye