URL Rewrite of Non-Existent File Under Subdomain Folder - apache

My website structure is as follows:
.htaccess
index.html
internal
.htaccess
test.html
api.php
I have setup a subdomain in GoDaddy such that internal.ledworld-me.com maps to ledworld-me.com/internal and the following works fine:
Requesting internal.ledworld-me.com/test.html serves the test.html file correctly.
Now I need the following:
If internal.ledworld-me.com/nonexistent/file is requested, then the URL should be rewritten as ledworld-me.com/internal/api.php?__route__=/nonexistent/file.
What do I put in my .htaccess file in order to achieve this? And which .htaccess file should I put the code in, the one under WebRoot or the one under internal?
Please note that GoDaddy is taking care of the rewriting from internal.ledworld-me.com to ledworld-me.com/internal. All I need my .htaccess file to do is the second part of the rewrite, namely, /nonexistent/file should be rewritten as api.php?__route__=/nonexistent/file.
It's really hard to explain this question and I've tried my best, please ask for clarification if needed. Thank you!

Put this code in your /internal/.htaccess file:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/?$ api.php?__route__=/$1 [L,QSA]

The following text in my internal/.htaccess file seems to do the trick:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://ledworld-me.com/internal/api.php?__route__=/$1
I'm still testing all the scenarios and will confirm that everything is working once I'm done all the tests.

Related

RewriteRule doesn't rewrite correctly

I'm hosting my development site on the localhost server and I used to access it at 127.0.0.1/dev. In the folder I have a .htaccess file containing following information. I'm new with the RewriteEngine and don't get this work.
RewriteEngine on
RewriteRule ^/(.*)$ /index.php?page=$1
When I'm trying to access 127.0.0.1/dev/home, I simply get a message that the page doesn't found. I can't see where rewrite is redirecting me, so I can't debug the problem in easy way. I think that you can see the problem at the first look.
Thanks in advance.
Try this rule in /dev/.htaccess without leading slash in source pattern and target URL:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php?page=$0 [L,QSA]

Apache: How do I re-route all requests to a single file?

I'm writing a static single page Backbone site with a single entry point: index.html
I've been told that because of this, I need to re-route all requests to my url e.g. www.example.com/*, to that index.html file. So, if someone types in www.example.com/lolnotreal, I need that request to be redirected to /index.html but in a manner which my index.html file still could pick up the url attempted, in the example's case: /lolnotreal
Is this even possible considering it's just an html file? Basically I need for backbone to pick up the url attempted.
Thanks for any help
Dearest downvoter: Please explain. I'm not a server expert and my hours of attempts at using .htaccess have failed.
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
that does it.
Try to put this lines to your .htaccess file
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/([a-zA-Z0-9\/\_]{1,})$
RewriteRule [^/]*/(([a-zA-Z0-9\/\_]{1,}){0,})$ /index.html?q1=$0 [L]

Folder is not visible in localhost

I have downloaded a web template and when i try to run that in my localhost the folder doesn't show up.that folder contains a .htaccess file.When i remove that file i can see the folder but when i open that it gives some errors.
This is the .htaccess file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^$ index.php [L]
RewriteRule ^([a-zA-Z0-9]+)?$ user-profile.php?user_username=$1 [NC,L]
this is the first time i'm working with a .htaccess file...
Any help is appreciated.
Maybe problem in the index.php file PHP code? Rewrite rules looks fine.
Show us the Apache error log and a rewrite log, without that hard to say what is wrong.
It looks like the RewriteCond directive is in the wrong place...? I would have thought it should apply to the 2nd RewriteRule, not the first:
RewriteEngine On
RewriteRule ^$ index.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-zA-Z0-9]+)?$ user-profile.php?user_username=$1 [NC,L]
RewriteCond directives apply to the single RewriteRule that follows. That 2nd ruleset basically says that if the requested file does not exist then internally rewrite the request to the user-profile.php page passing the request (which is assumed to be a username) to the user_username parameter.

htaccess and correct url

My problem is that I can't figure out why my htaccess doesn't work.
When I check FTP path it is /var/www/html/folder/subfolder/
Since I am running WP site and I need that subfolder to be accessible, so I have this:
RewriteEngine On
RewriteBase /folder/subfolder/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* ./index.php
I tried with adding html and of course removing folder but nothing is working.
Can anybody help me?
Thank you.
If you have httpd 2.2.16 or above, you should use FallbackResource instead of all those rewrite rules:
Put a .htaccess inside the folder you wish to have redirects for
Add FallbackResource /index.php
Have cake.

URL Rewriting on Localhost? Apache PHP

I am using Apache mod_rewrite on Xampp on my local machine but the code doesnt seem to work.
Please look at the code below and assist me. I'm a beginner with this.
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^m3/([a-zA-Z0-9-]+)$/?profile=com_frontpage&url=$1 [L,QSA]
What I want is the URL below:
http://localhost/m3/?profile=com_frontpage&url=nero
to be rewritten as
http://localhost/m3/nero
Please help me achieve this.
Thanx
Thanks for your quick responses
I have implemented the revised code given but once I try to enter the new URL I keep getting a 404 error.
I am using Xampp on a Windows OS so renaming the file to .htaccess is being rejected and I am not sure using 'htaccess.txt' is working.
What could be the issue?
Thanx
Change your RewriteBase.
RewriteEngine on
RewriteBase /m3/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9-]+)$ index.php?profile=com_frontpage&url=$1 [L,QSA]
EDIT: Fixed syntax.
Make sure you have this option set in your document root directory in Apache's httpd.conf file:
AllowOverride All
You want this if your .htaccess file is located in the site root (in the htdocs folder):
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^m3/([a-zA-Z0-9-]+)$ m3/index.php?profile=com_frontpage&url=$1 [L,QSA]
Also, you need to save your file as .htaccess. Just "Save As..." from whatever editor you're using and save the file as .htaccess. The only place that you can't do this from is within Windows Explorer, but that's just Explorer being stupid; you definitely can save the file with the correct name from a program (even Notepad).
Or, if you prefer, you can do it from command line:
cd C:\path\to\xampp\htdocs\
rename htaccess.txt .htaccess