Apache re-write URL path that doesn't exist - apache

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.

Related

htaccess rewrite root url to subfolder with accessing other folder?

I have following folder structure in apache server.
Public_html
-->admin
--->admin_login.php
-->website
--->index.php
since the index.php inside the website folder,
i have given following code in .htaccess
RewriteCond %{REQUEST_URI} !^/website/
RewriteRule ^(.*)$ /website/$1 [L,NC]
so that the when the user enter root url , it will appear "www.myurl.com" instead of "www.myurl.com/website/"
but the issue is, i could not be able to access admin_login.php.
is there anyway to modify .htaccess, to come website/index.php in main url and able to access admin_login.php(both)?
Thanks in Advance
You need to add an exception to existing rule like this:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/(website|admin)/ [NC]
RewriteRule .* website/$0 [L]
Negative condition %{REQUEST_URI} !^/(website|admin)/ will match every URI except URIs that start with /website/ or /admin/. This will allow you to directly open www.myurl.com/admin/admin_login.php.
With your shown samples and attempts please try following .htaccess rules file. Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^index\.php/?$ website/index.php [QSA,NC,L]

htaccess only one file in subfolder and keeping the url

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]

htaccess adding WWW and changing filename in subdirectory

I know similar questions have come up, though often without a working answer. I'm hoping to have better luck!
I have an .htaccess file in my root directory adding "www" to everything:
RewriteEngine on
RewriteCond %{SERVER_NAME} ^mysite.org
RewriteRule ^(.*)$ http://www.mysite.org/$1 [R=permanent,L]
This generally works fine. I have a subfolder (/myquiz/) in which the old index.html file has been replaced with index.php. I know there are external links to /myquiz/index.html, so I want to make sure those redirect. Leaving index.html in place and trying to redirect from that led to some odd behavior, but adding an .htaccess in that directory works for that:
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html?$ /myquiz/index\.php [NC,R]
Trying to load index.html redirects to index.php as hoped for, and the WWW gets added if needed. But requesting mysite.org/myquiz/index.php directly does not add the WWW.
I tried adding "RewriteEngine inherit", but that resulted in calls getting redirected to my root folder instead. A great trick if I want to make a subfolder inaccessible, but not helping here. I also tried just adding the code from my root .htaccess into the beginning of my subfolder's .htaccess, but that worked no better.
Any ideas?
You shouldn't need to add another htaccess file in the myquiz folder. This should work in the htaccess file in the root of the site. Remove the htaccess file in myquiz and try this.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mysite\.org
RewriteRule ^(.*)$ http://www.mysite.org/$1 [R=301,L]
RewriteRule ^myquiz/index\.html$ /myquiz/index.php [R=301,L]
Also I wouldn't use %{SERVER_NAME} unless your are sure the name is set properly in the config file. Then it can be more reliable than HTTP_HOST, otherwise I would instead use %{HTTP_HOST}.
I think inherit would work if you add an L flag to the rule that you have in your myquiz folder:
RewriteRule ^index\.html?$ /myquiz/index\.php [NC,R,L]
So that it redirects first, then the inherited rule (the www) gets applied after.
You could also just put both rules in the same file:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mysite.org$ [NC]
RewriteRule ^(.*)$ http://www.mysite.org/$1 [R=permanent,L]
RewriteRule ^myquiz/index\.html$ http://www.mysite.org/myquiz/index.php [R=permanent,L]

Url rewriting mod_rewrite

I have the url such as:
page.com/content.php?xname=p&yname=q&zid=1
I want to rewrite this url using apache mod_rewrite into something like:
page.com/p/q/
note there should not be 'zid' parameter in renamed url. I know expressions are passed as GET into the original url.
Is it possible to rename as above. If yes, How to achieve this?
This one works fine for me and will rewrite request for /p/q/ to /content.php?xname=p&yname=q&zid=1.
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/$ content.php?xname=$1&yname=$2&zid=1 [QSA,L]
This rule is to be placed in .htaccess in website root folder. If placed elsewhere some small tweaking may be required.
It will not rewrite if requested URL is a real file or folder (I'm sure you do not want to rewrite images or some other pages -- I had to add such condition since I do not know what is your website structure is).
RewriteRule ^content\.php\?xname=(p)&yname=(q)&zid=1$ /$1/$2 [R]
Instead of p and q you can try expressions like [a-Z0-9_-]+ to match identifiers.
There's an online testing tool here: http://civilolydnad.se/projects/rewriterule/
RewriteEngine On
RewriteRule ^/[a-z0-9]+/[a-z0-9]+/$ content.php?xname=$1&yname=$2 [L]

How to get file directory trough .htaccess by using `RewriteRule ^(.*)$ ?id=$1 [L,QSA]`?

How to get file directory trough .htaccess by using RewriteRule ^(.*)$ ?id=$1 [L,QSA]?
If .htaccess is located in http://localhost/some/dir/.htaccess and I'm opening http://localhost/some/dir/here/I/use/RewriteRule/, how I detect value /some/dir/ without using RewriteBase and without manual adding %{DOCUMENT_ROOT}/some/dir/, like value localhost I get trough %{HTTP_HOST}?
If you do not use RewriteBase you need to tell mod-rewrite the real Directory Root /var/ww/mysite/some/dir in the rewrite rule. RewriteBase would take the location url and map it to the directory.
So you'll maybe end up with
RewriteRule /var/ww/mysite/some/dir/(.*)$ ?id=$1 [L,QSA]
And trying to map some internal variables it may be
RewriteRule %{DOCUMENT_ROOT}/some/dir/(.*)$ ?id=$1 [L,QSA]
But I'm unsure, I rarely use mod_rewrite in .htaccess -- I prefer Directory tags, and the file path management can be different in .htaccess (auto removal and adding of directory prefixes). If you do not find a solution try to ask Servfault, plenty of admins other there.
Actualy Apache still does not have pathinfo($,PATHINFO_DIRNAME), function like has PHP.
So on now there are solution on using %{REQUEST_URI}, like this example:
RewriteRule ^(.+)/$ /path-dirname/$1 [R=301,L]
may reset with:
RewriteCond %{REQUEST_URI} ^(.+)/$
RewriteRule ^.+/$ %1 [R=301,L]