CakePHP: Can you globally modify all links to point to another directory? - .htaccess

My CakePHP app lives inside a subdirectory to keep it from crashing into a Wordpress installation that powers part of the website:
example.com/ <--root
/_wp <--Wordpress installed here
/page1
/page2
/_cake <--CakePHP installed here
/page3
/page4
To maintain consistency, I'm using mod_rewrite rules to rewrite URLs from example.com/_cake/pageX to example.com/pageX etc. This basically works, but CakePHP still creates links with /_cake/pageX. So if a user hovers over a link, the "_cake" will show up in the bottom of the browser and of course in the source code.
Is there a way to configure CakePHP to think it's actually in the site root so it creates the desired URLs for links etc?

I haven't found a way to configure CakePHP the way you want.
If you create your links with HtmlHelper::link or Router::url, you can add a member $url['base'] = false to the $url array argument. This prevents the _cake prefix being inserted in front of the URL.
As an alternative, you can create your own link() or url() function, which calls HtmlHelper::link and always adds base = false.
For this to work, you must also have a .htaccess in the document root, which rewrites the requests to their real destination
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ /_cake/$0 [L]
You must also pay attention to requests destined for the _wp directory.

Related

Why htaccess not working for mobile browser?

I have website (mzadkm.com) try to RewriteRule short url to app.php page .
So if user browse "mzadkm.com/app" will show "mzadkm.com/app.php" page
RewriteRule ^/app /app.php [L,R=301]
It's work on Computer , but on mobile browser give me 404 page
Any ideas
That probably is what you are looking for:
RewriteRule ^/?app /app.php [L]
The documentation clearly says, that the pattern in a RewriteRule get's applied to the relative path of the request if the rule is implemented inside a distributed configuration file. That means you actually want to match the path app and not /app here. Which is why your rule did not get applied. The ^/?app is a variant to accept both path notations, relative and absolut, which means the same rule can get implemented in the central configuration or likewise in a distributed configuration file (".htaccess").
I took the liberty to also remove the external redirection you showed ("R=301") since that most likely is not what you want, according to the phrasing of your question. Instead you want an internal rewrite .
You need to take care however that you do not implement a rewriting loop. Which would result in failing requests and an "internal server error" (http status 500).
One approach would be that:
RewriteEngine on
RewriteRule ^/?app$ /app.php [L]
Here another one:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?app /app.php [L]
Why things looked fine on your computer, but not on a mobile browser is unclear. Since the same rules get applied and the requests look the same there has to be another reason for that. I suspect you looked at a cached result of a previous attempt somewhere. Remember to always use a fresh anonymous browser window when testing. And to check the response you receive back inside your browsers network console.

htaccess RewriteRule leaving the current directory scope

I'm creating a website based on wordpress on a hosting system (unfortunately not a dedicated system in this situation) and I am very limited in my configuration opportunities for Apache2.4.x / PHP8.
I was unable to convince my client to move to a more advanced environment, so I have to work with the given playground, as follows:
There is a website in /www/ which is linked to the domain (A-Record). There's an old man that maintains the current website until the new website is finished.
I am really not afraid that the old dev accesseses my development scope intentionally or uses PHP to do so to cause harm (he doesn't know PHP, he uploads locally generated HTML). He's an old man and I'm rather afraid that he accidently deletes, overwrites or moves my work while I'm working on the new website and I have to put it back together. He might be like "oh I don't know that folder" and it's gone.
My first task was to make and install certificates and enforce HTTPS, that worked pretty well so far.
Now I need www.domain.tld/dev/ to show the wordpress site, however the old developer can access the www scope and I really don't want him to mess with my code. He barely knows HTML.
In opposite to him, I have full access and can go outside of the /www/ directory, so I created a /wordpress/. Unfortunately I have no option to add a subdomain for that on said host, either.
Now here's where my problem and my approaches kick in, I am unable to move /www/dev/ to show the /wordpress/ content which is not inside of /www/.
Theoretically I would do this on my root server but my client wants the website development to happen in his webspace. That's a bit strange but no subject to change. Just take that as given fact please.
So my htaccess rules for this are not doing anything and I can not spot what's wrong.
(Note: The .htaccess is laying inside of /www/)
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^domain.tld [NC]
RewriteRule ^(.*)$ https://domain.tld%{REQUEST_URI} [R=301,L]
#RewriteBase /
#RewriteRule ^/dev /is/htdocs/wpCUSTOMERIDSTRIPPED/wordpress [QSA]
#RewriteRule ^/dev/(.*) /is/htdocs/wpCUSTOMERIDSTRIPPED/wordpress/$1 [QSA]
RewriteBase /is/htdocs/wpCUSTOMERIDSTRIPPED
RewriteRule ^/www/dev /wordpress [QSA]
RewriteRule ^/www/dev/(.*) /wordpress/$1 [QSA]
Thank you alot in advance.
Note: The search function did not help me further as I am moving outside of the scope of the active directory.

htaccess redirect of root domain, not subfolders with url masking

I am trying to do the following -
Redirect just the root domain to a different domain.
The redirect needs to be masked so the user still thinks they are on the url they typed.
Existing subfolders should still work with the existing root domain.
For example-
I have an installation using www.currentsite.com which has lots of subfolders for example www.currentsite.com/store
I want to redirect just the root of www.currentsite.com to www.newsite.com but want the browser to still say www.currentsite.com.
If the user goes to www.currentsite.com/subfolder I still want that to work with the original installation.
I have the following which seems to be handling redirecting just the root fine but does not mask the url...
RewriteEngine on
RewriteCond %{HTTP_HOST} www.currentsite\.com [NC]
RewriteCond %{REQUEST_URI} ^/$
Rewriterule ^(.*)$ http://www.newsite.co.uk/ [L,R=301]
Any help id appreciated.
For what you call "masked" the usage of apaches proxy module makes most sense:
ProxyPass https://www.currentsite.com https://www.newsite.co.uk
ProxyPassReverse https://www.currentsite.com https://www.newsite.co.uk
It maps one base url to another one and takes care to transparently and reliably rewrite all contained references.
The proxy module can also be used by RewriteRules, the P flag does that. But in the end it comes out itself and the above, direct usage is more transparent and less complex.
Here is the documentation, as typical for the apache project it is of excellent quality and comes with lots of good examples: https://httpd.apache.org/docs/2.4/mod/mod_proxy.html

.htaccess slug url

I'm wondering if anyone will be able to help me, I'm trying to make a site using slug URLs.
At the moment if a user sees the url it is something like
http://www.thedomain.com/artists-single.php?aid=123
but ideally I would like the URL to be
http://www.thedomain.com/artist/artist-name
.
I have in the database a url friendly artist name which I would like to use.
At the moment within my .htaccess file I have the following:
RewriteEngine On
RewriteBase /
RewriteRule ^artists-single\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
The site itself is written with PHP.
Thanks.
You can get Apache to access MySql and map "artist-name" to it's corresponding ID, see here (Thanks to #Marc B for the link). However, you could also do something like this though (this is what I personally use),
RewriteRule ^artist/([a-zA-Z0-9]+)/?$ /artist-single.php?artist=$1 [L]
Then in PHP use $_GET['artist'] to get the value, then query that against the database to get the artist's ID. Or you could use the ID, like
RewriteRule ^artist/([0-9]+)/?$ /artist-single.php?aid=$1 [L]
The URL would be like www.example.com/artist/123, which would pass the id to $_GET['aid']
I would recommend that you make use of a very simple Front Controller. This will give you the least headache and should be rather simple looking at your current setup. Have a quick peak at this for more details: http://www.technotaste.com/blog/simple-php-front-controller/
But just to give you a quick starting point, a Front Controller will be "the first part" that gets executed, traditionally this was the "index.php" file. Using this Front Controller you can then have SEO-friendly URLs and not change any core functionally to your other files.

htaccess - redirect all requests within subdirectory, except if a requested file exists

I'm developing an app in php, and I need to set up a pretty broad .htaccess redirect. I did some reading and tried to write the RewriteConds myself, but it's a bit above my paygrade - I'm hoping someone with more experience can help. Here's what I'm trying to accomplish:
The app is contained in www.example.com/app/. Don't redirect anything above this directory.
Some files exist in this directory that will need to be accessed. Currently these are /app/includes/* and /app/sb_pages/*. This will change and expand in the future, so I need an elegant solution that encompasses all existing files. It's fine if the redirect triggers within these directories when a file isn't found - all I care about is being able to access the files within without the redirect triggering.
All other requests should be redirected to /app/index.php, with the trailing url passed in the querystring. For example, a request to /app/path1/path2/ should redirect to /app/index.php?path=path1/path2/
The redirect should not be transparent. When the user requests /app/path1/path2/, I want them to believe they have remained there. They should not see the url change to /app/index.php?path=path1/path2/.
Just for added clarity, here's a few cases to elaborate:
/app/includes/sidebar.php should not redirect.
/app/includes/nothing.html does not exist - redirect is OK
/app/path1/path2/ should redirect to /app/index.php?path=path1/path2/. User should still see their current URL as /app/path1/path2/.
I hope I've explained it clearly and pre-empted most questions. If you need clarification, please don't hesitate to ask. Thanks in advance for the help!
Try adding this to your .htaccess file in your document root:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^app/(.*)$ /app/index.php?path=$1 [L,QSA]
Note that if you want accesses to existing directories (as opposed to files) to also not be redirected, add a RewriteCond %{REQUEST_FILENAME} !-d above the rule.

Resources