I've got a URL structure where there's a gallery page at /worcester/gallery which has an index.php file within. What I want is to have all child URLs of this, like /worcester/gallery/last-night to run the index.php file within /worcester/gallery.
I'm not looking to redirect the URL, I want to keep the URL as it is, I'll be using that to work out which gallery to get from index.php
Since you want to access the URI in php, you can simply put the following as .htaccess inside gallery/ directory:
RewriteEngine On
RewriteRule !^index\.php index.php [L]
I am not sure though, that REQUEST_URI will be available inside the index.php file. If it is, well and good; if it isn't, you can pass the rest of the URL as a GET parameter:
RewriteEngine On
RewriteRule ^((?!index\.php).+)$ index.php?uri=$1 [L]
Related
Please could I have some help redirecting URLs to a subfolder so that everything in https://example.com is rewritten to https://example.co.uk/subfolder?
The .htaccess file needs to be in /subfolder.
You could do something like the following in the root .htaccess file to rewrite all direct requests to the subfolder.
RewriteEngine On
# Rewrite all direct requests to the `/subfolder` directory
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^ subfolder%{REQUEST_URI} [L]
The check against the REDIRECT_STATUS environment variable ensures that only direct requests from the client (as opposed to internally rewritten requests) are rewritten to the /subfolder directory (thus avoiding a rewrite loop).
This also means that a direct request for /subfolder itself will also be rewritten to the /subfolder directory (ie. /subfolder/subfolder) - so you can have a sub-subfolder of the same name if you wish (otherwise direct requests for /subfolder will result in a 404). That is, unless you have an additional .htaccess file in the subfolder that also contains mod_rewrite directives, in which case these directives will be overridden.
UPDATE: Following your question update that now states the following...
The .htaccess file needs to be in /subfolder.
In that case, this is not possible. The .htaccess file that rewrites requests to the /subfolder must be in the document root (the parent directory).
If the .htaccess file is in the /subfolder itself then it will simply never be processed for requests outside of that subfolder in order to rewrite the request.
MrWhite already gave nearly all the explanation I should have attached to my answer. So, like he already stated in his answer, the directives given below should be written into the .htaccess file that is located in the folder where https://site1.co.uk points to for it to work as you expect.
Here are the directives you can use.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /subfolder/$1 [QSA,L]
</IfModule>
The first RewriteCond will help make sure that the request is not for a file that already exists in the folder that the .htaccess file is located.
The second RewriteCond line is similar to the first but this checks for a folder instead. After this line, you can, and probably should, add the REDIRECT_STATUS line from MrWhite's answer. See his answer for the explanation.
The RewriteRule line uses Regular Expressions (RegEx) to effect the rewrite. Remember to replace subfolder on this line to name of the folder you wish to use and this folder should exist in the same location that the .htaccess file is located, and like already explained, this must be the folder that https://site1.co.uk points to.
Hope this helps.
suppose, I have an index100.php file inside folder1 (eg. folder1/index100.php). Is there any way so that if the user types folder1/index.php, then it will automatically redirect to folder1/index100.php?
How can it be done using .htaccess file?
If you want to show index100.php insted of index.php as your directory handler, you can use DirectoryIndex directive in /folder1htaccess
DirectoryIndex index100.php
This will serve you the /index100.php if you visit http://example.com/folder1
If you actually want to redirect /folder1/index.php to /folder1/index100.php then use the following rule in your /folder1/. htaccess .
RewriteEngine on
RewriteRule ^index\.php$ /folder1/index100.php [L,R]
hi i read all of other questions but i cant resolve my problem.
i setup my joomla website in this url: https://www.example.com/new
and now i want reditect this to (https://www.example.com) with my data for example i want reditect example.com/new/all-ads-smart/103-Hotels or other to example.com/all-ads-smart/103-Hotels
also i test this but it dont work
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^new/(.*)$ https://www.example.com/$1 [R=301,L]
this just work when i open (https://www.example.com/new) and redirect me to (https://www.example.com) and when open this (https://www.example.com/new/ads.php) i give 404 error
in end i sorry for my bad english<3
my data is:
new/all-ads-smart/103-Hotels
to
/all-ads-smart/103-Hotels
RewriteRule ^new/(.*)$ https://www.example.com/$1 [R=301,L]
This actually looks OK and should "work", providing it is in the root .htaccess file and located near the top of that file. However, if you still have a .htaccess file in the /new subdirectory then this is likely to override the directive in the parent .htaccess file - so it won't do anything.
However, it would be preferable to do this in the /new/.htaccess file instead (and keep the parent .htaccess file clean). For example, remove everything in the existing /new/.htaccess file and simply have an unconditional redirect:
RewriteEngine On
RewriteRule (.*) https://www.example.com/$1 [R=301,L]
This will override the parent .htaccess file and when anyone visits the /new subdirectory they will be redirected. (This uses mod_rewrite, as opposed to a mod_alias Redirect to ensure that it overrides the parent .htaccess file.)
I want to use .htaccessto redirect different requests to the same folder.
E.g.:
domain.de/ordner1/fileX.html
domain.de/en/folder1/fileX.html
domain.de/it/casella1/fileX.html
So whenever something is requested out of /ordner1/, /folder1/ or /casella1/ I want .htaccess to fetch the requested file out of a specific directory like domain.de/all/fileX.html.
I want to prevent duplicate content but also keep the foldernames in the selected language.
Could you help me solve this problem?
Try adding the following to the .htaccess file in the root directory of your site.
RewriteEngine on
RewriteBase /
#skip css, js etc
RewriteCond %{REQUEST_URI} !\.(css|js)[NC]
#if request to ordner or folder1 or casella1, serve the file from all/
RewriteRule ^(ordner1|en/folder1|it/casella1)/(.+)$ all/$2 [L,NC]
In your docroot/.htaccess:
RewriteEngine On
RewriteBase /
RewriteRule ^(ordner1/|en/folder1/|it/casella1/)(.*$) all/$2 [L]
You would need to add extra names to map other translation equivalents.
I have an htaccess file that I want to redirect the all pages to the another folder. I am using cakephp for the site, except for this folder which I want to redirect.
"/app/webroot/" is added to the new directory so the url is /app/webroot/new/ instead of just new.
I am placing this htaccess file (below) in the "new" directory and want anything at /app/webroot/new/ to redirect to /new/ and remove the /app/webroot/ This folder is independent of cakephp and thus does not need to be processed by cake.
The code below loops and I am not sure why.
Options +FollowSymlinks
RewriteEngine On
RewriteBase /new/
# If your concerned about direct access to a particular page without the sub-dir
# you will want to add something like this
RewriteCond %{REQUEST_URI} !^/new
RewriteRule (.*) /new/$1 [R=301,L,NC]
You should NOT place the .htaccess file in /new/ but in /app/webroot/new/.