I'm trying to write a rewrite rule to redirect files in my new webhelp system (it's a stand alone website).
I have two things I try to take into account -
I want to redirect only files that are in the http://www.mydomain.com/webhelp folder.
I need to change the .html in the end to .htm...
I manage to find each rule by itself by matching the http://www.mydomain.com/webhelp, but
when I try the rewrite cond it breaks...
Basically, its
http://www.mydomain.com/webhelp/hello.html
into
http://www.mydomain.com/webhelp/hello.htm
without changing
http://www.mydomain.com/index.html
Would really appreciate any help.
Thanks!
If you put the following in a .htaccess file in your webhelp folder, it should achieve what you're looking for:
RewriteEngine on
RewriteBase /webhelp/
RewriteRule ^(.*).html$ $1.htm [R=301,L]
You need to use backreferences. Something like:
RewriteRule ^www/webhelp/(.*)[.]html([#])(.*)[.]html$ www/webhelp/$1.htm$2$3.htm
RewriteRule ^www/webhelp/(.*)[.]html$ www/webhelp/$1.htm
Related
I can access my web server as follows: https://www.example.com/my_old_folder/some_folder/
There's an .htaccess file in /my_old_folder/ with the following code:
RewriteEngine on
RewriteRule ^my_old_folder/(.*) my_new_folder/$1
I want to rewrite the folder my_old_folder internally to my_new_folder, without changing the URL in the browser. Just grab the files from /my_new_folder/ instead of /my_old_folder/. If there's another folder like /some_folder/ in this case, keep it. Only change the name /my_old_folder/ to /my_new_folder/.
Unfortunately, it's not rewriting the path, although I already tried many solutions from the internet, including the above one.
Who can help?
Inside /my_old_folder/.htaccess you can use this rule:
RewriteEngine on
RewriteRule .* /my_new_folder/$0 [L]
It is because all path matching is relative to my_old_folder/ inside /my_old_folder/.htaccess.
I would like to restructure some folders on my website, specifically I am want to move what's contained inside "images/" to "images/gallery/", but I don't want to break previous links, so I thought of using htaccess.
I looked up several tutorials and even several questions here on stackoverflow, tried several times, but I can't get the rewrite rule to work.
This is what I have:
RewriteRule ^images/(.*) /images/gallery/$1 [R=301,NC,L]
But when I try to access anything inside /images/ (for example images/test.jpg) it stays into images/test.jpg and doesn't go to images/gallery/test.jpg. So it doesn't seem to have an effect.
Any clue on what I might possibly doing wrong?
Thank you!
Your rule at present will cause a redirect loop since /images/ is present in both source and target URLs and you're not even using anchor $:
You can tweak your regex like this:
RewriteRule ^images/([^/]+)$ /images/gallery/$1 [R=301,NC,L]
Now pattern will match /images/test.jpg but won't match redirected URL /images/gallery/test.jpg due to use of [^/]+ in pattern.
Make sure this rule is first after RewriteEngine On and there is no .htaccess in /images/ folder.
EDIT: If your original path has sub-directories also then use:
RewriteRule ^images/((?!gallery/).+)$ /images/gallery/$1 [R=301,NC,L]
I''m sorry for asking the millionth Apache rewrite question here. I tried everything I know, but there is a small (hopefully) step that I'm looking for someone to shed a light for me.
I have a URL structure similar to this:
- assets
- assets/dist/19854/css/my.css
- css/my.css
I'm trying to rewrite assets/dist/19854/css/my.css file to css/my.css file in the root.
I have mod_rewrite enabled on my server, and I have basic understanding of rewrite rules, but it would be great if you could assist me with the Regex.
RewriteCond %{REQUEST_URI} ^assets/dist/([0-9/.]+)/(.*)$
RewriteRule ^assets/dist/([0-9]+)/(.*)$ $ [L,QSA]
Problem with the above rule is the it rewrite to ./19854, but I'm actually interested in the second expression's match. If possible, I'd also like to make sure the css/my.css file exists first.
Thanks in advance!
Edit:
Thanks for the comments and the answer. To further explain my case, this is a small site that uses a CDN, and everytime a new build is up, the number in assets/dist/[0-9] gets changed, so all assets' source URL gets changed. But I'm using a CSS compiler to compile CSS files, so CSS files reside in the same folder (css/my.css).
I have some other rewrites so I'm making my RewriteConds more strict.
So far, the above rewrite matches the numeric part, but I'm trying to rewrite to the URL right after the numeric part.
I believe you're attempting in other way round. You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
# route to /css/my.css if it exists
RewriteCond %{DOCUMENT_ROOT}/$1 -f [NC]
RewriteRule ^assets/dist/.+?/(css/my\.css)$ $1 [L,NC]
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 really can't understand how to write htaccess lines. I was trying to Google for few hours and couldn't find anything relevant. Can anybody suggest me a simple .htaccess line that can let me navigate to http://www.mydomain.com/mydirectory/index5.html
by calling it as http://www.mydomain.com/required
I tried in the below manner but I didn't work for me.
RewriteEngine On
RewriteRule ^required mydirectory/index5.html [NC,L]
And, an other question if I place this .htaccess file in the mydirectory folder will that work or am I supposed to place this file only in the root folder?
Thanks.
Put this code in .htaccess in DOCUMENT_ROOT:
RewriteEngine On
RewriteBase /
RewriteRule ^required/?$ /mydirectory/index5.html [NC,L]
PS: You cannot keep this code in /mydirectory since your original URI doesn't contain /mydirectory
Reference: Apache mod_rewrite Introduction