I have the following in my htaccess:
RewriteRule ^news/[a-zA-Z0-9-_]+?/(.+)$ /news [R=301,L]
This works how I want it to, for example if I go to /news/some-category/some-post it just redirects to /news which is great. However it is also affecting my uploads folder. Take the following URL for example:
/news/wp-content/uploads/2014/07/Emily-286x300.jpg
This gets redirected to /news as well so all my images are broken. Is there a way to tweak this rule so that it doesn't affect the wp-content/uploads directory?
If /news/some-category/some-post represent a 3-level subdirectories, you can use this rule
RewriteRule ^news/[^/]+/[^/]+/?$ /news [R=301,L]
Also, please note you'll have to clear your browser's cache before trying again such link /news/wp-content/uploads/2014/07/Emily-286x300.jpg (my rule will not match it but your last rule does and it's in browser's cache)
Related
I have a url like
domain.com/order/index.php?q=
the order folder no longer exists as its been changed to ecom, so it now looks like the below.
domain.com/ecom/index.php?q=
The problem is we have a ton of external links wanting the order url some scripts and images pull from this url, etc.
How can I simply set .htaccess to still allow the order directory.
I tried this
RewriteRule ^order/(.*)$ /ecom/$1 [L]
It works only if it exactly matches order (e.g domain.com/order/ , but anything after order it then breaks (e.g domain.com/order/index.php?q=)
Any help would be appreciated. I just keep seeing examples similar to what I have above so not sure if I am doing something wrong here.
Use QSA flag
RewriteRule ^order/(.*)$ /ecom/$1 [QSA,L]
you can do a permanent redirect to a page
RewriteRule ^url-string-to-redirect$ http://www.yourdomain.com/your-new-url-string [R=301,L]
I have found many 301 htaccess redirects examples but i need something specific and dont know how to build that exact redirect rule... the site i am build this for is a wordpress site.
Example:
http://www.example.com/article/some-article-01.html
http://www.example.com/article/data-analysis.html
http://www.example.com/article/another-page.html
To redirect to:
http://www.example.com/article/some-article-01/
http://www.example.com/article/data-analysis/
http://www.example.com/article/another-page/
looking for one htaccess rule that works in wordpress...
a pointer to relevant data would be accepted hapily. cant seem to find the right search query. An example code would be of course much more easier.
Insert this rule in your DOCUMENT_ROOT/.htaccess file as very first rule just below RewriteBase line:
RewriteEngine On
RewriteBase /
RewriteRule ^(article/[^.]+)\.html$ /$1/ [L,NC,R=302]
If it works for you then change 302 to 301 to make it permanent redirect.
We have a website where we show clients creative work we have produced for them. We upload raw assets to a path like this:
x.com/clients/clientName/campaignName/size/
I have a PHP script which adds our branding, contact information and other information and pulls in the raw creative (usually a swf object). It is in this directory x.com/clients/index.php and it accepts a query string parameter ?path so it knows where to look for the creative.
I am trying to do an apache rewrite in .htaccess so that our designers can upload directly to the known folder structure but so that when you go to x.com/clients/clientName/campaignName/size/ it should rewrite to x.com/clients/index.php?path=clientName/campaignName/size/
I am currently using the following rewrite rule, which works for the first folder level e.g. x.com/clients/clientName/ does successfully rewrite, but any subsequent folders do not.
RewriteRule ^clients/([^/\.]+)/?$ /clients/index.php?path=$1 [L]
My RegEx's are terrible, so I'm stuck on what to do. Any help appreciated, thank you kindly.
Your regex is only matching urls like clients/xxxxxx/ because your pattern [^/\.]+ means one or many characters except "/" or "."
With your rule, it can't work for other subdirectories.
You can change your rule by this one
RewriteRule ^clients/(.+)$ /clients/index.php?path=$1 [L]
To avoid internal server error (code 500 which means an infinite loop in this case), you can do it this way
RewriteRule ^clients/index\.php$ - [L]
RewriteRule ^clients/(.+)$ /clients/index.php?path=$1 [L]
Is there a special reason you want to use regex? In my opinion you can just catch everything coming after /clients:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^(.*/)?index\.php$ [NC]
RewriteRule ^clients/(.*)$ /clients/index.php?path=$1 [L]
The second line is to prevents redirect loops, because the index.php is also in the folder /clients and this would cause never ending redirects.
I have a situation where an entire folder's contents are no longer needed and will be redirected to the home page, except 6 or so files. The folder holds over 300 files, so individual redirects:
redirect 301 /folder/file.html http://www.domain.tld/
redirect 301 /folder/file2.html http://www.domain.tld/
redirect 301 /folder/file3.html http://www.domain.tld/
This would take quite a long time. I have some time before needing this done, and would like to know if anyone knows a good way to achieve this by using a little regex with mod_rewrite.
For optimum understanding for all who may use the potential correct answer, lets say the files that we don't want to redirect are:
/folder/stay1.html
/folder/stay2.html
/folder/stay3.html
Thanks in advance for this wonderful community of very knowledgeable people helping those of us who still have a few things to learn!
Edit
Is it possible to achieve this and keep the base url of the folder?
/folder/
/folder/index.html
I tried the following without success:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/folder(/|/index.html|/stay1.html|/stay2.html|/stay3.html|/etc.html)
RewriteRule ^/?folder/ http://www.domain.tld/ [L,R=301]
Edit Correct Answer
A big thanks goes out to Jon Lin for the answer.
The correct method to redirect all files of a /folder/ except a few, while still allowing access to /folder/ is:
RewriteEngine On
# Allow /folder/ to remain accessible
RewriteCond %{REQUEST_URI} !^/folder/$
# Allow specified files to remain accessible
RewriteCond %{REQUEST_URI} !^/folder/(index.html|stay1.html|stay2.html|stay3.html|etc.html)
# Redirect all non-specified files to home page
RewriteRule ^/?folder/(.+)$ http://www.domain.tld/ [L,R=301]
Using mod_rewrite, you can create exception conditions to the redirect, try putting these rules in the htaccess file in your document root:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/folder/$
RewriteCond %{REQUEST_URI} !^/folder/(index.html|stay1.html|stay2.html|stay3.html|etc.html)
RewriteRule ^/?folder/(.+)$ http://www.domain.tld/ [L,R=301]
So anything that's in the list: (stay1.html|stay2.html|stay3.html|etc.html) will fail the condition and the redirect won't happen. Otherwise, anything starting with /folder/ will get redirected to http://www.domain.tld/.
Note that if you have mod_alias redirects intermixed, they may interfere with each other.
You could use RedirectMatch with a negative lookahead, like:
RedirectMatch permanent ^/?folder/(?!(stay1\.html|stay2\.html|stay3\.html)) http://domain.tld
An alternative mod-rewrite solution would be like this:
RewriteRule ^/?folder/(stay1\.html|stay2\.html|stay3\.html)$ - [L]
RewriteRule ^/?folder/.* http://domain.tld
The first rule catches all the exceptions, the L flag ensures no further processing takes place in this pass, and the - instructs the engine not to rewrite, ensuring no further passes are made. Anything not caught by the first rule is redirected by the second rule.
my .htaccess file contains the following
RewriteCond %{HTTP_HOST} ^www\.mydomain\.org\.in [NC]
RewriteRule ^(.*)$ http://mydomain.org.in/$1 [R=301,L]
I moved the whole site to a subfolder and now none of the css and js files in the webpage load. Can anybody tell me what this regex means or why this is happening?
Note: I inherited the site from my seniors :P
It just redirects any request to www.mydomain.org.in/... to mydomain.org.in/...; i.e. it strips the www from the front. However, this shouldn't cause the resource files to break if you simply move it to a subdirectory, assuming you've moved them as well (though you should probably leave the .htaccess file where it is).
It sounds like the links to your CSS/JS files in your HTML might be broken, perhaps because they use absolute URIs (relative to the domain root rather than the current URI). Try checking them first.
As Will explained the .htaccess is not the issue. Your JS and CSS locations were mentioned not relatively and as such when the location of the source files changed they are not being found by the browsers and as such the page is not rendering.
However, you can try the following .htaccess code in addition to the one you are having and see if it links to the files.
RewriteRule ^(.+)\.css$ http://mydomain.org.in/folder/$1.css [R=302,NC]
RewriteRule ^(.+)\.js$ http://mydomain.org.in/folder/$1.js [R=302,NC]
The above code redirects calls to css and js files to a subfolder in your domain. Change folder to the folder you moved everything to.