I'm using an htaccess rewrite that looks like this:
RewriteRule ^library/.*(\.pdf)$ email/$1 [L,R=301]
The problem is, the redirect works but does not contain the filename of the file being redirected:
http://mydomain.com/library/.pdf
Is there something wrong with the way I'm setting up the rewrite rule?
RewriteRule ^library/(.*)\.pdf$ email/$1 [L,R=301]
if you want just the name, otherwise
RewriteRule ^library/(.*)\.pdf$ email/$1.pdf [L,R=301]
for the full name
Related
I would like to make a redirect using htaccess rules to point ALL links from
mywebsite.com/subfolder1/subfolder2/...whatever-path....
to
mywebsite.com/subfolder1
But the problem is that whatever rule I use it always keeps the old path in the destination link, for example:
mywebsite.com/subfolder1/subfolder2/mypage.php
becomes
mywebsite.com/subfolder1/mypage.php
I tried all the following possibilities:
RewriteRule (.*) https://mywebsite.com/subfolder1/$1 [R=301,L]
RewriteRule ^subfolder1/subfolder2/(.*)$ /subfolder1/$1 [R=301,NC,L]
RewriteRule ^/subfolder1/subfolder2/(.*)$ /subfolder1/$1 [R=301,NC,L]
Redirect 301 /subfolder1/subfolder2 https://mywebsite.com/subfolder1
RedirectMatch 301 /subfolder1/subfolder2(.*)$ /subfolder1/$1
RedirectMatch 301 ^/subfolder1/subfolder2/?$ https://mywebsite.com/subfolder1
I have placed the "Options +FollowSymLinks" and "RewriteEngine on" rules before them, but NONE of them worked.
Can someone tell what's wrong and how to find the right rule?
Thank you.
Try this
RewriteRule ^subfolder1/subfolder2/(.*)$ /subfolder1 [R=301,NC,L]
With $1you attach the content of (.*) to the redirected URL. Remove them from code and
http://mywebsite.com/subfolder1/subfolder2/whatever-path
will be
http://mywebsite.com/subfolder1
I'm looking for the following to change the URL with two variables in a .htaccess file, from:
domain.com/folder/a123/url/index.php
domain.com/otherfolder/a321/url/index.php
to:
domain.com/folder/url/index.php
domain.com/otherfolder/url/index.php
I would prefer it in a single line .htaccess RewriteRule, if possible.
"folder" and "otherfolder" are the only two "folder names" and "a123"/"a321" can be anything starting with "a" and ending with random numbers.
You can do that in one with just one rule using this:
RewriteEngine on
RewriteRule ^(.+)/a[0-9]*/url/index.php$ /$1/url/index.php [R=301,NC]
But if you want to redirect all pages and not only index.php then use this:
RewriteRule ^(.+)/a[0-9]*/url/(.*)$ /$1/url/$2 [R=301,NC]
Also, if you want to rewrite the URLs instead of redirect them change [R=301,NC] to [NC].
Note that if you have that pattern in other URLs this rule will also apply, for example it will also redirect domain.com/adifferentfolder/a567/url/index.php to domain.com/adifferentfolder/url/index.php, if you have those type URLs and don't want to redirect them you need to use two rules instead:
RewriteRule ^folder/a[0-9]*/url/(.*)$ /folder/url/$1 [R=301,NC]
RewriteRule ^otherfolder/a[0-9]*/url/(.*)$ /otherfolder/url/$1 [R=301,NC]
My htaccess file is under "myappname" folder.
I'm trying to redirect this path;
myappname/CONTROLLER_NAME/ANYFILE.(css|js|gif|jpg|png)
To that path
myappname/views/default/tpl/CONTROLLER_NAME/ANYFILE.(css|js|gif|jpg|png)
and this is my HTACCESS rule
RewriteEngine on
RewriteRule ^(.*)/(.*)\.(css|js|gif|jpg|png)$ views/default/tpl/$1/$2.$3 [L,NC]
but it's giving me HTTP 500 unless I redirect it to a PHP file like
RewriteEngine on
RewriteRule ^(.*)/(.*)\.(css|js|gif|jpg|png)$ views/default/tpl/$1/index.php?a=$2.$3 [L,NC]
What is wrong with my rule? I'm very new to htaccess and there is a very big potential to I'm missing something small.
This is because your target matches the regex:
views/default/tpl/foo/bar.png
matches the regex:
^(.*)/(.*)\.(css|js|gif|jpg|png)$
So the rules just keep looping. You need to add a condition:
RewriteCond %{REQUEST_URI} !^/views/default/tpl
right above your RewriteRule.
I try to change over htaccess from
http://www.mydomain.com/pictures/generated/product/1/280_280_75/nopic.jpg
to
http://www.mydomain.com/pictures/generated/product/1/280_280_75/nopic1.jpg
Here is my rules
RewriteCond %{HTTP_HOST} ^mydomain\.com
RewriteRule nopic\.jpg$ nopic1\.jpg
It works in some way;) The whole URL will be changed just to "nopic1.jpg".
The question is: How can I change only the last part of URL?
This should work:
RewriteRule ^(.*)nopic\.jpg$ $1nopic1\.jpg
I want accesses to e.g. www.thisdomain.com/docs/path1/path2 to redirect to www.thatdomain.com/path1/path2
(Note that docs is not a part of the new path)
I have the following on www.thisdomain.com:
RewriteEngine on
RewriteRule ^docs/* http://www.domain.com/ [R=301,L]
If I access www.thisdomain.com/docs, it directs to www.thatdomain.com, but if I access a child-path like www.thisdomain.com/docs/path1/path2 it fails. Is it possible for the redirect to intercept the child-path access and redirect as I need? If so, any pointers?
Thanks.
With regular expressions, * means any number of the previous atom, which will match /docs and /docs/. Try this:
RewriteEngine on
RewriteRule ^docs$ http://www.domain.com/ [R=301,L,QSA]
RewriteRule ^docs/(.*) http://www.domain.com/$1 [R=301,L,QSA]
(QSA is query string append, so /docs/foo?bar=baz won't lose the ?bar=baz.)
According to section "Redirect Old domain to New domain using htaccess redirect" of the first Google result which I found searching for "htaccess redirect" (without the double quotes), this piece of code will suffice:
Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]
According to their description of the technique, it will redirect the directory the .htaccess file is placed in recursively (including any child paths), just as you intend. Of course, mod_rewrite needs to be present for the rewrite directives to work.