Redirect all subfolder's to parent folder - .htaccess

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.

Related

I need to .htaccess redirect my subfolder to the root folder, except two folders

The subfolder i want to redirect to the root folder is www.example.com/subfolder
So I need to redirect everything in that subfolder to the url adress https://www.example.com except for these two folders (which are placed in the subfolder): www.example.com/subfolder/folder1 and www.example.com/subfolder/folder2
I spent like 4 hours trying to find the exact code but I could not solve that. Nothing worked for me.
I've tried many codes, but nothing worked for me. For example:
RewriteEngine On
RewriteCond %{REQUEST_URI}!^/subfolder/folder1/
RewriteCond %{REQUEST_URI}!^/subfolder/folder2/
RewriteRule ^(.*)$ /$1 [R=301,L]
and
RewriteEngine on
Options +FollowSymlinks
RewriteCond %{REQUEST_URI}!^/subfolder/folder1/
RewriteCond %{REQUEST_URI}!^/subfolder/folder2/
#RewriteRule (.*) http://www.example.com/subfolder/ [R=301,QSA,L]
RewriteRule (.*) https://www.example.com [R]
Could someone help me with that?
I'd prefer to place the .htaccess file in the subfolder.
Thank you so much.
I'd say that your first attempt looks pretty good. Just make a slight modification since you say you want to redirect to the root path, not something inside the root path:
RewriteEngine On
RewriteCond %{REQUEST_URI}!^/subfolder/folder1/
RewriteCond %{REQUEST_URI}!^/subfolder/folder2/
RewriteRule ^(.*)$ / [R=301,QSD,L]
Since you want to use a distributed configuration file (as opposed to the preferred central configuration for the http host) that rule is meant to be used inside a ".htaccess" file inside the "subfolder".
It is a good idea to start out using a R=302 temporary redirection and to only change that to a R=301 permanent redirection once everything works as intended. Also you need to make sure that you always test using a fresh anonymous browser window to prevent client side caching effects.
And you also need to make sure that such distributed configuration files are considered at all by the http server for that location (see the documentation of the AllowOverride directive).

How to hide folder in url bar

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]

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 redirect user upon specified HTTP HOST

Due to some internal problems and refusal from another part, I need a way to redirect ALL access from a specified domain. So far I've come up with the following:
RewriteCond %{HTTP_HOST} ^bad-domain.com [NC]
RewriteRule ^(.*)$ http://mydomain.com/bad-request.html
Which doesn't work. Worth noting is that I'm not good with rewrites, but I'm trying to learn.
I've sucessfully implemented this in PHP, but that requires the code in every project, which really isn't the way to go.
All suggestions, tips and answers are appreciated that puts me in the right direction.
I don't see a reason why this should not work. Please check these points again:
RewriteCond %{HTTP_HOST} ^bad-domain.com [NC,L]
RewriteRule ^(.*)$ http://mydomain.com/bad-request.html
As Alexander Støver pointed out:
RewriteEngine on
Then, if you put those rules in your servers configuration you have to restart the daemon. So probably something like
/etc/init.d/apache2 restart
Make sure you check the error logfile if the daemon complains about something. Should be something like this:
/var/log/apache2/error_log
or wherever you write your logs to.
If you put those rules into ".htaccess" files (why?) then make sure the server is actually configured to use those files and that you allow to override file paths:
AllowOverride: FileInfo
Use logging to debug the rewriting. There are two options for this provided by mod_rewrite:
RewriteLog
RewriteLogLevel
You also need to add a rule for images.
RewriteCond %{REMOTE_ADDR} !^187\.10\.226\.42
RewriteCond %{REQUEST_URI} !^/maintenance\.html$
RewriteCond %{REQUEST_FILENAME} !.(gif|jpe?g|png)$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/maintenance.html [R=307,L]

htaccess / mod_rewrite problems with possible subdirectories

I am working on a very dynamic system where i have two identical htaccess files in / and in /somepath. The reason for this that the domain could be pointed into /somepath, but i never know if it is.
When it is pointed to /somepath there are no problems, but when its not it seems like when i request /somepath/page/foo/bar the htaccess file in /somepath overrides the one in /. In the latter case i dont want the /somepath/.htaccess to run at all, or at least disregard the mod_rewrite in it.
One solution would be if i could check if the latter htaccess is not located in the document root. Is this possible? How can i compare the htaccess path to document root from within the htacces file?
Both htaccess files look like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_FILENAME} \.php$
RewriteRule .* uri_handler.php [L]
</IfModule>
Does anyone know whats going on here?
Thanks!
I don't think this is possible. But this sounds like a construction error either way. It would be much better to incorporate the two .htaccess files into one. (Or is the second one in /somepath even necessary? Why?)
Under which circumstances does the request's domain point to /somepath? Is it a different domain you could use in a RewriteCond of its own?
Barring any attempts at simplifying your setup, I think that the easiest approach here is to just make sure that the .htaccess file in /somepath doesn't handle requests to /somepath when the document root is /.
Assuming we're all on the same page about the document root, we can accomplish this by modifying /somepath/.htaccess:
Edit: The easier way is to just have it always request the uri_handler.php that lives at the root:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_FILENAME} \.php$
RewriteRule .* /uri_handler.php [L]
</IfModule>

Resources