URL Rewriting on Localhost? Apache PHP - apache

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

Related

.htaccess matches files even without extension in URL

.htaccess
RewriteEngine On
# If directory, redirect to root
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ / [R,L]
# Don't show something.json
#RewriteCond %{REQUEST_FILENAME} -f
RewriteRule something\.json$ / [R,L]
# If not file or directory, go to the router
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ /subfolder/router.php [L]
# Any other existing files get served normally
We can ignore the first two rules, they work. The problem is in the 3rd part/rule.
htaccess file is here:
https://subdomain.sitename.com/subfolder/.htaccess
I need any non-directory and non-file addresses to go to /subfolder/router.php.
I have a html file on the server:
https://subdomain.sitename.com/subfolder/sub2/filename.html
I want the URL:
https://subdomain.sitename.com/subfolder/sub2/filename
to map to /subfolder/router.php, since filename doesn't exist on the server (only filename.html exists).
But it serves/shows the filename.html file/page instead of mapping to /subfolder/router.php. Address bar still shows just filename without .html.
It works fine on my localhost, but not on the online server I uploaded the files to. I don't have access to the config files of the server.
I've read online, and looks like I'm on a virtual host, maybe?
I've tried:
Options -MultiViews (gives error)
AcceptPathInfo off (doesn't help)
RewriteCond %{REQUEST_FILENAME}.html -f (nope)
... some other desperate ways of matching
Is there any way to force RewriteCond %{REQUEST_FILENAME} !-f to not match extensions of files on the server when they are not in the URL?
Or at least somehow fix the problem?
I'm losing my mind right now, after hours of trying things and reading online. Any help would be greatly appreciated.
Okay, after some thinking and trying things out, I solved it by changing:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ /subfolder/router.php [L]
to
RewriteRule \.html$ /subfolder/router.php [L]
I'm not entirely happy, but it works for now, as the router.php file checks for file existence and uses a whitelist of files.

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 Rewrite of Non-Existent File Under Subdomain Folder

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.

htaccess Rewrite to same name file in different folders of site

I tried to search a lot but got no success. I wish that someone could really help me out with this.
Ok, so my website is like:
/rewrite.php
/folder1/rewrite.php
/folder2/rewrite.php
/folder1/subfolder1/rewrite.php
/folder1/subfolder2/rewrite.php
and so on...
In short, there exist a rewrite.php in each and every folder(except admin access folder).
So basically, I want to rewrite to the rewrite.php file in the corresponding directory, but I am not able to do it.
That is
If I request
/News
then it should rewrite it to:
/rewrite.php
If I request:
/folder1/News
then it should rewrite it to:
/folder1/rewrite.php
and so on for other folders.
Currently, this is what I have in my htaccess:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . rewrite.php [L,NC]
Somewhere, I read that If I remove RewriteBase, then it uses the current directory as RewriteBase. But it failed and didn't worked.
Also, I would like to keep some folders in exceptions(in which Rewriting should be disabled) like:
/administration
/config
I hope for a feasible solution, if anyone can help me... :)
Best Regards,
Ankur Thakur
Try:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/(administration|config|rewrite.php)
RewriteRule ^(.*?)(/?[^/]+)$ $1/rewrite.php [L]

mod_rewrite fails at string "asdf", all other tested strings work - why?

.htaccess
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ index.php?u=$1 [L]
Url localhost/some_text is rewritten to localhost/index.php?u=some_text. When trying many different strings, by accident I found that "asdf", meaning localhost/asdf redirects to the default apache page (I am using XAMPP, so xampp home folder).
Has anybody an idea why?? Is there sth wrong with anything in .htaccess? Or would you too consider this a strange behaviour?
PS: there is no directory named asdf nor any file or extension asdf in this folder nor anywhere...