How do I create dynamic .htaccess file that recognizes the server path. It needs to work on localhost folder which path is:
/Library/WebServer/Documents/myscaryproject/.htaccess
and on hosted server path which is:
/domains/mydomain/.htaccess
This is my .htaccess currently:
ErrorDocument 404 /myscaryproject/404.php
RewriteEngine on
# removing index.php from url
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ /myscaryproject/index.php?event=$1 [L]
# removing index.php and replacing it with event and page parameter
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)$ /myscaryproject/index.php?event=$1&p=$2 [L]
RewriteRule ^login/?$ login.php [NC]
This all doesn't work if I push it to the server, because pathing isn't correct.
Related
I'm running a web app in azure and I can only use .htaccess to achieve the behaviour I'm looking for: if the requested URL doesn't match a file under /app or /dist folder then redirect the request to /dist/main.php. This works well if I try something like: http://example.com/ (the request goes into /dist/main.php without any URL redirect) but if I instead use http://www.example.com then it returns a 301 to http://example.com/dist/main.php and I have no idea why?
.htaccess content under wwwroot:
<IfModule mod_rewrite.c>
RewriteEngine on
Options +FollowSymLinks
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/dist%{REQUEST_URI} -f
RewriteRule .* dist/%{REQUEST_URI} [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/app%{REQUEST_URI} -f
RewriteRule .* app/%{REQUEST_URI} [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* /dist/main.php [L,QSA]
</IfModule>
I also tried the last line as: RewriteRule ^ /dist/main.php [L,QSA]
I want to use .htaccess files so that if someone visits an old URL on our new website such as website.com/oldpage.asp .htaccess first checks to see if website.com/oldsite/oldpage.asp is on our server. And if no file is there redirect them to old.website.com/oldpage.asp where a copy of our old website exists.
For some reason only the .htaccess file in the /oldsite/ folder is executing.
Here is my .htaccess file for my site root which checks if the file exists on the new site and if not redirects to /oldsite/
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /oldsite/$1 [NC,QSA,L]
Then in /oldsite/ its supposed to check if the file again exists, and if not redirect again.
RewriteEngine On
RewriteOptions inherit
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://old.website.com/$1 [R=301,NC,QSA,L]
You don't need 2 .htaccess for this.
Have this rule in site root .htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/oldsite/$1 -f
RewriteRule ^(.*)$ oldsite/$1 [NC,QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://old.website.com/$1 [R=301,L]
Then remove /oldsite/.htaccess and test this after clearing your browser cache.
I have an hidden folder hidden_folder which can be accessed by the server with
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ hidden_folder/$1
Now, I want to redirect all unavailable pages to 404 error page content.
Illustration 1 : When user tries to go here http://website.com/some_unavailable_uri, the url of the page doesn't change but the content will be the one of /error/404.php
Illustration 2 : http://www.apple.com/test. This page does not exist, the url does not change, but the content displays error.
Basically, it should be if file does not exist, then redirect content. I'd tried something but with no success.
First try :
RewriteEngine On
RewriteCond hidden_folder%{REQUEST_URI} -f # %{REQUEST_URI} already has "/" at its beginning
RewriteRule ^.*$ error/404.php [NC,L]
Second try :
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}hidden_folder%{REQUEST_URI} -f
RewriteRule ^.*$ error/404.php [NC,L]
Any idea ?
Looks like you just need a simple ErrorDocument with use of http:// in target since that causes URL to change in the browser.
Use this line top of your .htaccess:
ErrorDocument 404 /error/404.php
And don't forget to comment out your rewrite rules doing same.
The conditions are tested also on redirects, so you must make sure that the 404 redirect doesn't catch more than it should.
What this means is that %{REQUEST_URI} will contain hidden_folder once it's been redirected, so you must add a RewriteCond to avoid it.
This should work:
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/hidden_folder%{REQUEST_URI} !-f
RewriteCond %{REQUEST_URI} !^/hidden_folder
RewriteRule ^(.*)$ error/404.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ hidden_folder/$1 [L]
Thank you Outlyer.
The actual working code is :
RewriteEngine On
# If the requested file is not in hidden_folder as a file
RewriteCond %{DOCUMENT_ROOT}hidden_folder%{REQUEST_URI} !-f
# AND
# If the requested file not is in hidden_folder as a folder
RewriteCond %{DOCUMENT_ROOT}hidden_folder%{REQUEST_URI} !-d
# AND
# If the requested file is not in root as a file
RewriteCond %{REQUEST_FILENAME} !-f
# AND
# If the requested file is not in root as a folder
RewriteCond %{REQUEST_FILENAME} !-d
# AND
# ???
RewriteCond %{REQUEST_URI} !^/hidden_folder
# THEN Redirect content
RewriteRule ^(.*)$ error/404.php [L]
####################################################
# Enable the deletion of hidden_folder in urls
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ hidden_folder/$1 [L]
I was wondering if it's possible to redirect all requests to files/directories that do not exist to index.php, and after that clean the url.
So when I go to www.example.com/test/1 I want it to be redirected (rewrote?) to www.example.com
I tried the following and it does correctly load index.php, but it still says www.example.com/test/1 in the URL bar...
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [L]
Of course I still want my css and js to load properly...
Try this. If I understand, you want to use this as a way of redirecting a 404 not found?
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [R=301,L]
Or maybe this without index.php
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ / [R=301,L]
i want to rewrite url using .htaccess file,
my all data is saved in work folder on server
and i want to show that folder when user enter the complete webaddress
www.example.com to www.example.com/work using mod_rewrite
my code is
RewriteRule ^(.*)$ /work/$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
this code create problem when i enter somthing like www.example.com/folder/ than it externally redirect me to www.example.com/work/folder/ and shows 404 page not found even when work contains that folder and that folder contain index.php
Try this :
RewriteCond %{REQUEST_URI} !^/?work/
RewriteRule ^(.*)$ /work/$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ $1.php [NC,L]