Rewrite A Filename To Another Filename Via .htaccess - .htaccess

I wanna change "announcements.php" to "blog.php" in the url. Individual article's are identified by id=x, such as"annoucements.php?id=1", which I will need it to say "blog.php?id=1".
I tried:
RewriteRule ^announcements.php ./blog.php

Try with:
RewriteEngine on
RewriteRule ^announcements\.php /blog.php [NC,L]
If you change the file name in the url, you do a redirection, not a rewrite.

Related

how to convert exrenal link to internal using .htaccess?

i want convert all external link to internal and redirect them
let me show you what i want:
i have an external link to my website like this:
link
and i converted it to:
link
OR
link
OR somthing like this
how can i use .htaccess to make sure when someone opened the link he goes to:
https://external.com/some text here
i tried this but id didn't worked
RewriteEngine On
RewriteRule ^mywebsite.com/redirect/(.*)$ /$1 [NC,L]
RewriteRule ^mywebsite.com/redirect/(.*)$ /$1 [NC,L]
RewriteRule only matches against the path component of the URL. (And when configured in .htaccess context, the path it checks against never starts with a leading slash, the path to the current directory has been stripped off at this point already.)
/$1 would cause a slash before the actual substitution URL.
Both fixed, it should simply look like this:
RewriteRule ^redirect/(.*)$ $1 [L]

I want to use a RewriteRule in .htaccess to allow subfolders

We have a url mydomain.com/events that needs to include a country code just before th events folder like this mycomain.com/uk/events. The country could be any number of different codes. I want to use a RewriteRule like:
RewriteRule ^([a-zA-Z0-9-/]+)/events/*.* /events/*.*
so any any url like these will work:
mydomain.com/uk/events
mydomain.com/uk/events/index.php
mydomain.com/uk/events/list-events.php?s=12
With your shown samples/attempts, please try following htaccess rules file. Please make sure to clear your browser cache before testing your URLs. Place your htaccess file along with your domain folder(not inside it beside it).
RewriteEngine ON
RewriteCond %{THE_REQUEST} \s/([^/]*)/(?:[^/]*)/([^/]*)(/.*?\.php(?:\?[^=]+=\d+)?)?\s [NC]
RewriteRule ^ %1/%2%3 [L]

htaccess redirect not including filename in destination

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

Need to .htaccess redirect path with a variable URI segmant to another directory without changing URL

My current .htaccess looks like this:
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteRule ^[0-9]+/pos /pos/$1 [L]
Currently this will redirect a url such as example.com/234234234/pos to example.com/pos
I would like it to load the directory example.com/pos, but without losing the original URL (example.com/234234234/pos) from the address bar.
Basically, the number listed in the url can change, but I always want it to load the same path.
This rule doesn't look right:
RewriteRule ^[0-9]+/pos /pos/$1 [L]
As you're not capturing anything hence there is no $1. If you want to ignore any number before /pos then use this rule:
RewriteRule ^[0-9]+/(pos)/?$ /$1 [L,NC]
Thank you anubhava. Part of your answer helped me. I did want the numbers ignored, but didn't want them to disappear.
This is the line I needed:
RewriteRule ^[0-9]+/pos/ /pos/ [L,NC]
Did not need to capture anything at all. That is what was causing the redirect.

.htaccess 301 redirect path and all child-paths

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.

Resources