I need the URL example.com/app to show the content from example.com/app/dist
The following code does exactly this.
RewriteEngine On
RewriteRule ^$ /app/dist/ [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/dist/
RewriteRule ^(.*)$ /app/dist/$1
BUT I also need the URL example.com/staging to show the content from example.com/staging/dist
With the current setup, the url example.com/staging is showing the content from example.com/app/dist
The .htaccess while is located in the /app and /staging folder (but it has to be the same file)
In the htaccess file in your staging folder, change all instances of app to staging
Edit:
You can try using relative paths instead of hardcoding the app or staging, but Ive had issues in the past, especially if there are other rules in play.
RewriteEngine On
RewriteRule ^dist - [NC,L]
RewriteRule ^$ dist/ [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ dist/$1 [L]
The idea is to remove the leading / from rewrites so a relative path is used. So technically it wont matter whatever folder the htaccess file is in
Related
Hello I have been stuck with htaccess for the past 3 days and can not seem to figure out why my htaccess file is not changing my url correctly.
So I am attempting to turn this query string url from www.example.com/test/user-profile.php?id=4 into www.example.com/test/user-profile/4
the /test/ in the url is not a file, its a folder in the main public_html directory.
Here is my htaccess file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-l
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ /test/user-profile.php?id=$1 [L]
RewriteCond %{QUERY_STRING} ^id=([0-9]*)$
RewriteRule ^(.*)$ /test/user-profile/%1? [R=301,L]
As of right now it is going to the url www.example.com/test/user-profile/4 but it is not loading the entire page. Just beyond stuck, thank you for your time
With your shown samples, attempts; please try following htaccess rules file. Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteBase /
##RewriteBase /test/
##Extrenal redirect rule.
RewriteCond %{THE_REQUEST} \s/(test/user-profile)\.php\?id=(\d+)\s [NC]
RewriteRule ^ %1/%2? [R=301,L]
##Internal rewrite rule.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/(.*)/?$ $1/$2.php?id=$3 [QSA,L]
JS/CS rewrite/redirect:
You may need to use base tag to fix your js and other relative resources. If you are linking js files using a relative path then the file will obviously get a 404 because its looking for URL path. for example if the URL path is /file/ instead of file.html then your relative resources are loading from /file/ which is not a directory but rewritten html file. To fix this make your links absolute or use base tag. In the header of your webpage add this <base href="/"> so that your relative links can load from the correct location.
I have an existing site with a bunch of pages in /widgets/ and the new site also has pages in /widgets/, some of which correspond to existing URLs. However, lots of other pages in that widgets section no longer exist and I just want to redirect them to /widgets.
Basically, I want to write a rule that catches pages that are not found and redirect them to the landing page (/widgets/) whilst allowing legit pages to be found.
Is it possible to write rules that will do this or do I need to manually redirect them all?
EDIT: As this is using a CMS (Craft) there are existing rules going on:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/(favicon\.ico|apple-touch-icon.*\.png)$ [NC]
RewriteRule (.+) index.php?p=$1 [QSA,L]`
UPDATE: Since these pages only exist in the CMS itself and not as actual files on the filesystem then the CMS is also going to have to manage the redirection. Apache (.htaccess) does not know what is or is not a valid page, so unless you can determine a (filename) pattern to these redirects, you would need to specify each file/redirect manually in .htaccess.
Since this is an external redirect, it should come before your existing rules:
RewriteEngine On
RewriteBase /
# Redirect non-existent files within /widgets/ to /widgets
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^widgets/. /widgets [R=301,L]
# Existing rewrites
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/(favicon\.ico|apple-touch-icon.*\.png)$ [NC]
RewriteRule (.+) index.php?p=$1 [QSA,L]
Note that the RewriteRule checks for the /widgets/ directory, not RewriteCond directive - which would be less efficient.
I assume you already had a RewriteBase directive in your original code? This is required since you have a relative path substitution in your RewriteRule.
You can catch missing files in htaccess with -f
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f // if not a file
RewriteCond %{REQUEST_FILENAME} !-d // if not a directory
RewriteCond %{REQUEST_URI} /widgets/(.*) // if in directory /widgets
RewriteRule ^.*$ /widgets [L] // redirect to /widgets
You can leave out the third line if you want to redirect missing pages outside the widgets sub directory.
I've got Apache 2.4.4 running on Windows 8 laptop (WAMP server) and I found a really odd behavior of .htaccess mod_rewrite rules.
I want to redirect the root of my website to a specific file. My .htaccess looks like this:
RewriteEngine On
RewriteBase /
RewriteRule $^ /static/home.php [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ router.php?url=$1 [L,QSA]
This works only if there's no index.php file in root directory. When I create index.php file, Apache redirects empty path straight to the index file and doesn't bother with the first RewriteRule.
Is there a way to have both these RewriteRules and index.php file working together? In oher words, my example works but I want to rename router.php to index.php and keep both RewriteRules working.
Thanks!
You can just tweak your regex pattern to work in both situation:
DirectoryIndex something-else.php
RewriteEngine On
RewriteRule ^$ /static/home.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [L,QSA]
I have a CMS installed at the root of my domain with the following htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
This takes care of friendly urls to redirect everything to index.php. My problem is that I have a sub-folder which has another cms. I need the requests to the sub-folder be redirected to the proper folder instead index.php.
I tried the following but it doesn't seem to work
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(sub-folder)
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
Can anyone help?
Thanks
You should verify properly if request uri match the sub folder
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !/subfolder
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
In any case, the first two rewritecond means: if request file name is not a file or a directory. Which means that if the file you are referencing is into the sub folder and the sub folder is into your docroot, you don't need to do anything.
If still won't work, enabling the log could help you:
RewriteLog "/web/logs/mywebsite.rewrite.log"
RewriteLogLevel 9
RewriteEngine On
avoid this log in production
Using a high value for Level will slow down your Apache server dramatically! Use the rewriting logfile at a Level greater than 2 only for debugging!
I'm using codeigniter and .htaccess rewriting. Currently it's rewriting all non-existing file queries to index.php(CodeIgniter), like so:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php?/$1 [L]
Now i'd like like to rewrite urls beginning with "images" to "application/images" if file doesn't exist. Something like this:
RewriteCond %{REQUEST_URI} ^/images
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ ./application/$1 [L]
Well, that works in root directory, but not in subdirectory.
I do not want to add subdirectory name to htaccess, i'd like it to be dynamic. So if i'll make copy of this dir, and rename it, i dont want to change my htaccet to this dir.
RewriteRule is relative to the directory im working but RewriteCond is not? Thanks in advance :)
Problem is solved by this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(images|js|css)(.*)$ ./application/$1$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(images|js|css)(.*)$ ./application/$1$2 [L]