How to hide folder in url bar - .htaccess

I need to hide folder name in my url bar.
Now I have mateotxt.pl/website/regulamin.html
But I need
mateotxt.pl/regulamin.html
I need to skip this folder name in my url bar.

This probably is what you are looking for:
RewriteEngine on
RewriteRule ^/?regulamin\.html$ /website/regulamin.html [END]
In case you are looking for a general rule that might be of help:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/website/
RewriteRule /website%{REQUEST_URI} [END]
Though you need to take care that you do not actually rewrite resources you do not want to get rewritten that way. So maybe resources stored in other folders in the server side file system...
You should implement those directives in the actual http server's host configuration. If you do not have access to that you can use a distributed configuration file (often named ".htaccess"), but that comes with a performance penalty...

you can try the following:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^website/
RewriteRule ^(.*)$ website/$1 [L]

try this
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/string/
RewriteRule ^string/(.*)$ https://www.domain.eu/$1 [L,NC,R=301]

Related

Redirect all subfolder's to parent folder

Sorry, I do not understand in htaccess and all, what i do, dont work for me.
Please tell me how to redirect from all these subfolders (with one rule):
/about/vacancy/sys-admin/
/about/vacancy/lalala/
/about/vacancy/master/
/about/vacancy/lol/
To parent folder:
/about/vacancy/
What i try:
#1
RewriteRule ^about/vacancy/(.*)$ /about/vacancy/ [L,R=301]
#2
RewriteCond %{REQUEST_URI} !^/about/vacancy/.+$
RewriteRule .* /about/vacancy/ [L]
Your second variant should work, but the first approach is less complex. You just have to slightly adapt it:
RewriteEngine on
RewriteRule ^/?about/vacancy/.+$ /about/vacancy/ [L,R=301]
In case you are using a distributed configuration file (".htaccess") and not the real http server's host configuration you need to enable those first. Take a look at the documentation of the AllowOverride for that.

RewriteRule does not match if folder name set to "myfolder/"

Since we migrated to a new server, some of our pages are broken (404). Reason is we have 2 broken rewrite rules.
What's really strange is that they work if I change folder's name.
For example this work:
RewriteRule ^anything/([a-zA-Z0-9-]+)/$ page.php?var=$1 [L]
This doesn't:
RewriteRule ^myfolder/([a-zA-Z0-9-]+)/$ page.php?var=$1 [L]
I can't even find a trick to make 301 redirects, because my original "myfolder/" virtual folder never matches.
Any ideas what's going on? I was thinking it could be a rule override or something like that (as it's hosted on a multidom solution), but i don't have such rules in my main site at the root. It drives me crazy.
Thx!
In practice you probably want to do 2 things. Disable multiviews and also bypass rules if the request is a real directory.
Options -MultiViews #turn off automatic URI matching, can cause weirdness
RewriteEngine on
#stop here if the request is a real file or directory
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^myfolder/([a-zA-Z0-9-]+)/?$ /page.php?var=$1 [L]

htaccess subdomain rewrite without a redirect

Using htaccess Rewrite, I want my url http://*.phoneataxi.com/ (where * is a wildcard, excluding 'www') to show in the address bar as is but get information from http://*.phoneataxi.com/test.php?c=*.
I have tried so many different things but nothing is doing exactly what I need. Most examples are redirecting the subdomain to the '/test.php' file in the address bar which I don't want to do.
I'm trying not to have to create individial subdomains and subdomain folders within my webroot.
Ideas?
I use this htaccess file to make Apache act as a proxy for another host:
IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^ghost\.pileborg\.se$
RewriteRule (.*) http://vps.pileborg.se/ghost/$1 [P]
</IfModule>
It causes all access to http://ghost.pileborg.se/ to be "redirected" to http://vps.pileborg.se/ghost/.
UPDATE (2020)
Some of the answers regarding this topic is very old and no longer work as expected.
After searching for hours on something that actually works, this is what I came up with; edit as you see fit:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ([a-z0-9]+)\.
RewriteRule ^(.*)$ - [E=BASE:%1]
RewriteCond %{DOCUMENT_ROOT}/%{ENV:BASE}/index.php -f
RewriteRule ^(.*)$ %{ENV:BASE}/index.php [L,NC,QSA]
RewriteCond %{DOCUMENT_ROOT}/%{ENV:BASE}/index.html -f
RewriteRule ^(.*)$ %{ENV:BASE}/index.html [L,NC,QSA]
Breakdown
Make sure that the rewrite module is installed and enabled on your host
first we turn the rewrite engine on and set the path-base
then isolate the subdomain - any letters/numbers before the first dot
set a variable in this runtime environment that contains the subdomain
check if the subdomain folder and index-file exists
if it does exist -then use that file as the request-handler (no redirect)
if it does not exist then the request carries on normally
Flags
The flags used here are explained here, but the ones used above are quite simple:
[L] Last rule, ignore the rest
[NC] No Case, no uppercase/lowercase restrictions
[QSA] I remember this as "Query String Attach" :D

.htaccess redirect from one subfolder to other subfolder

I know this sounds like so many other questions here, but I can't seem to find the answer.
Say you are on:
www.domain.com/folderA/folder2/folder3/
I want that to redirect to:
www.domain.com/folderB/folder2/folder3/
So the whole structure stays the same.. it just redirects.
Now so far I have:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/folderA [NC]
RewriteRule ^(.*)$ /folderB/$1 [R=301,L]
But when I use that, it'll just do
www.domain.com/folderB/folderA/folder2/folder3/
What am I doing wrong? How do I get rid of that folderA?
The pattern ^(.*)$ includes also the prefix folderA. You must specify folderA explicitly in the pattern and capture only the latter part in the RewriteRule. Then you can drop the RewriteCond
RewriteEngine on
RewriteRule ^/?folderA/(.*)$ /folderB/$1 [R,L]
Never test with 301 enabled, see this answer Tips for debugging .htaccess rewrite rules for details.

.htaccess stupid issue

I have a server where I can only configure httpd using .htaccess, a cannot access global configuration. I want to rewrite almost every non-existing path to index.php, so I did something like:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?path=$1 [L,QSA]
And that is ok. But another thing I want to do is to prevent access to path where I keep PHP files (I cannot store them somewhere else) which is for example /php. I would like that anything starting with /php rewrites to index.php?path=php... so I did something like:
RewriteCond %{REQUEST_URI} ^/php.*
RewriteRule ^(.*)$ /index.php?path=$1 [L,QSA]
And that also would work ok if I had access to global configuration. In case of per-directory configuration, when I access /php it rewrites it to index.php?path=php and than puts directory name before that: /php/index.php?path=php. I read documentation and I realize that it uses internal redirects when per-directory confoguration is used. How can I avoid this behavior?
You can use Deny from all instead of mod_rewrite, but if you really want it, you can force external redirects as noteed in the mod_rewrite docs:
If a substitution string starts with
http://, then the directory prefix
will not be added

Resources