htaccess only one file in subfolder and keeping the url - apache

I need to put files in a new folder like this:
domain.com/newsite/newpanel/events.php
There are other files in the server like:
domain.com/events.php,
domain.com/oldsite/events.php etc.
Requirement 1:
I cannot redirect everything (because old implementations exist) with generic rules,
so I only want to redirect specific urls.
domain.com/events should now skip the old files and go to domain.com/newsite/newpanel/events.php
Requirement 2:
I tried something like this
RewriteRule ^events /newsite/newpanel/events/$1 [P]
but the url on the url bar will change. Is it possible for it to display domain.com/events?
thank you all!

EDIT: Since OP has mentioned different URLs in comments section so adding solution as per that here.
RewriteEngine ON
RewriteCond %{REQUEST_URI} ^/CProjects/events/?$ [NC]
RewriteRule ^(.*)$ CProjects/folderA/folderB/events [L]
Based on your shown samples, could you please try following. Please make sure you clear your browser cache before testing your URLs after putting these Rules into your .htaccess file.
RewriteEngine ON
RewriteCond ^/events/?$ [NC]
RewriteRule ^(.*)$ newsite/newpanel/events.php [L]

Related

Dynamically hide all sub folder but initial

Essentially, I need to edit the .htaccess file to develop a way to hide folder structure after /file. For example, if I have this URL:
https://www.example.com/file/page/work/assignments.php?
I should only see https://www.example.com/file/assignments.php?
How can this be accomplished?
So far I have tried the following, but it does hide all folders after first:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$
RewriteRule !^subfolder/ /subfolder%{REQUEST_URI} [L]
You can do something like the following to "hide" page/work after the /file/ directory and before the last path segment.
This needs to go in the root .htaccess file.
RewriteEngine On
# Redirect "/folder/page/work/<file>" to "/folder/<file>"
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(folder)/page/work/([^/]+)$ /$1/$2 [R=302,L]
# Rewrite "/folder/<file>" back to "/folder/page/work/<file>" (if it exists)
RewriteCond %{DOCUMENT_ROOT}/$1/page/work/$2 -f
RewriteRule ^(folder)/([^/]+)$ $1/page/work/$2 [L]
The will externally redirect a request for /folder/page/work/assignments.php?something to /folder/assignments.php?something. And internally rewrite the request back again.
NB: You should already be linking to /folder/<file> internally. The initial redirect to remove page/work is just for SEO if you are changing an existing URL structure. Otherwise, this is not strictly necessary.
A look at your existing rule...
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$
RewriteRule !^subfolder/ /subfolder%{REQUEST_URI} [L]
This would seem to be unrelated to what you are trying to achieve. It would internally rewrite a request for /<anything> to /subfolder/<anything>, essentially "hiding" /subfolder.
There is no need to check the requested Host header unless you are accepting requests to multiple domains and this is specific to just one of those domains. (You've made no mention of this in your question.)

how to dynamically rewrite filepath .htaccess based on domain?

I have this apache rule in my .htaccess file:
RewriteCond %{HTTP_HOST} ^testURL.localhost.local
RewriteRule ^index.php$ /_testURL/index.php [L]
How would I write this so that testURL2.localhost.local or any other subdomain would be rewritten to the corresponding directories?
For instance testURL2.localhost.local would be rewritten to _testURL2/index.php etc
I already tried the option below but I didn't get the intended result:
RewriteCond %{HTTP_HOST} ^testURL
RewriteRule ^cms.php$ /_%1/cms.php [L]
With your shown samples, attempts; please try following htaccess rules file. I have posted 2 sets of htaccess rules file here, you have to use ONLY one set at a time.
1st solution: This is specifically for host testURL.localhost.local and will look for either index.php OR cms.php only in UI.
RewriteEngine ON
RewriteCond %{HTTP_HOST} ^(?:www\.)?(testURL)\.localhost\.local$ [NC]
RewriteRule ^((?:index|cms)\.php)$ _%1/$1 [NC,L]
2nd solution: A Generic solution, where it will look for anyvalue.localhost.local and it will add anyvalue in path while rewriting, also it will look for any php files in uri.
RewriteEngine ON
RewriteCond %{HTTP_HOST} ^(?:www\.)?([^.]*)\.localhost\.local$ [NC]
RewriteRule ^([^.]*\.php)$ _%1/$1 [NC,L]
NOTE1: Also please make sure to clear your browser cache before testing your URLs.
NOTE2: Please keep these rules at the top of your htaccess rules file.
NOTE3: I am also additionally/optimally matching www. in host in case you don't want it you could remove it (?:www\.)? part in condition.

Apache re-write URL path that doesn't exist

We currently have a website with a URL structure as follows:
https://www.example.com/en_CA/homepage/page1.html
https://www.example.com/en_CA/homepage/page2.html
We need to shorten the URL to:
https://www.example.com/page1.html
https://www.example.com/page2.html
We have tried using the following rewrite rules and conditions:
RewriteCond %{REQUEST_URI} !^/en_CA/homepage/ [NC]
RewriteRule ^/(.*)$ /en_CA/homepage/$1 [P]
The problem we have is that we get a 404 because the shorter URL doesn't exist. I think the solution needs to also involve AliasMatch to set up an alias for that URL but I'm not sure how to go about that.
I've tried:
AliasMatch ^/[^/]*/(.*) /en_CA/homepage/$1
RewriteCond %{REQUEST_URI} !^/en_CA/homepage/ [NC]
RewriteRule ^/(.*)$ /en_CA/homepage/$1 [PT]
But this doesn't work.
The website is build using Adobe AEM so we need to ensure that AEM only ever receives the long URL.
Thanks
Russell
There is no need to use AliasMatch, I think, you want to access the url https://www.example.com/en_CA/homepage/page1.html from https://www.example.com/page1.html, and the same to the other one. Please let me know if I am wrong.
Try this, let me know if it works:
Please read the comments (text after # symbol) carefully
# Add this to your root .htaccess file i.e the public_html, htdocs, etc. or use RewriteBase /
RewriteEngine On # Do not use this two times in one .htaccess file, be sure you don't have any other directories other than /en_CA/homepage/ in your root dir, or use the RewriteRule ^(.*)$ /dirname/$1 [L] for every dir.
RewriteCond %{REQUEST_URI} !^/en_CA/homepage/
RewriteRule ^(.*)$ /en_CA/homepage/$1 [L]
I am sure the above will work.
Follow the same to other folders.
I will add something later to hide the folder containing it.

Apache Redirects - .htaccess - ReWriteCond

I'm trying to manipulate the .htaccess file on my blogs server to allow certain content to be redirected while allowing others to be accessed.
What I would like to do, is allow any file with the extension of (gif|jpg|css|js|ico|png) to be accessed along with a directory /includes/ and a file sitemap.html while all other traffic I would like to send to the sitemap.html file.
Is this possible?
Here's the code that I'm trying to use but it doesn't appear to be working as ecpected. The redirect matched the file extensions and allows the sitemap.html file to be accessed but the /includes/ directory that I want to be allowed access to froma user perspective regardless of what files are in the actual directory still appears to be redirecting to the sitemap.html file.
RewriteEngine On
RewriteCond %{REQUEST_URI} !(\.(gif|jpg|css|js|ico|png)$|^/includes$|^/sitemap\.html$)
RewriteRule ^(.*)$ /sitemap.html [R=301,L]
Any help would be greatly appreciated.
Try this:
RewriteCond %{REQUEST_URI} !\.(gif|jpg|css|js|ico|png)$ [NC]
RewriteCond %{REQUEST_URI} !^/(sitemap\.html|includes(/.*|))$ [NC]
RewriteRule ^ /sitemap.html [R=301,L]
Sure both RewriteCond lines can be combined into 1 but I would suggest keeping it 2 separate as this is easier to read and easy to maintain in case you want to add new extensions or new directories for exclusion in future.

Proper htaccess rewrite

I'm trying to force an old URL to go to the new url and my code seems to have no effect
RewriteCond %{QUERY_STRING} ^index2\.php?page=shop\.product_details&\.tabs\.tpl&product=310&category=71&Itemid=2$
RewriteRule .* /hunting/back-packs/multi-packs/black-2.5-pack.html [R=301,L]
my old url is
www.mywebsite.com/index2.php?page=shop.product_details&.tabs.tpl&product=310&category=71&Itemid=2
and my new one is
www.mywebsite.com/hunting/back-packs/multi-packs/black-2.5-pack.html
My code does not break anything but does not work either
Thanks in advance
As I understand you want redirect (301 Permanent Redirect) so the URL will change in browser. This will work for this URL ONLY /index2.php?page=shop.product_details&.tabs.tpl&product=310&category=71&Itemid=2:
RewriteCond %{QUERY_STRING} =page=shop.product_details&.tabs.tpl&product=310&category=71&Itemid=2 [NC]
RewriteRule ^index2\.php$ http://www.mywebsite.com/hunting/back-packs/multi-packs/black-2.5-pack.html [R=301,L]
If you want internal redirect (rewrite), then use these lines:
RewriteCond %{QUERY_STRING} =page=shop.product_details&.tabs.tpl&product=310&category=71&Itemid=2 [NC]
RewriteRule ^index2\.php$ /hunting/back-packs/multi-packs/black-2.5-pack.html [L]
PLEASE NOTE:
You need to put these lines in a proper order (order of rules matters) otherwise (if you put it at the end) some another rule will rewrite it to a different URL.
This needs to be placed in .htaccess file in website root folder. For any other location you may need to modify it a bit.