What is a magic with 5 lines of code in htaccess? - .htaccess

I want to know, with this code in .htaccess
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
Can do many things on query strings from URLs like Wordpres have done by example. It's depend on our coding or.. ? What is the secret ingredients to make like that?
Let me know if you have a good source to share with me. All in one.

This would make some kind of catch all page.
Let's explain it line by line
RewriteEngine On
enable mod rewrite
RewriteBase /
Set the base url of the rules to '/'
RewriteCond %{REQUEST_FILENAME} !-f
if the requested file doesn't exist (i.e not a jpg)
RewriteCond %{REQUEST_FILENAME} !-d
if the requested directory doesn't exist
RewriteRule . /index.php [L]
Redirect everything to index.php
So to summarize it will redirect any URL to index.php if the filename or directory requested doesn't exist.
and the documentation if you want to do some more reading.

The rewrite engine makes it possible to match paths and do manipulations, redirects etc. based on advanced rules.
Your specific example sets up two conditions (path must not represent an existing regular file nor directory) and then redirects anything else to index.php. You can then implement whatever you wish in index.php in order to match and/or parse the original request (e.g., /news/some-title could be forwarded to the news module where the news item can be fetched via the slug).
Sometimes you will see the initial redirect rewrite(s) done directly in .htaccess. The above could look like:
RewriteRule ^news/(.+)$ newsview.php?title=$1 [QSA,L]

Related

Mod Rewrite append .php to string in URL

OK so I've found every rendition of this but not this specifically. So my urls are in this format
www.inspection.com/users/?action=register
I want to convert it to
www.inspection.com/users.php?action=register
Here's what I've got so far in my htaccess
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^[A-Za-z0-9]+/\?action=[A-Za-z0-9]+$ $1.php?action=$2 [L,QSA]
But error logs say RewriteRule: cannot compile regular expression
Main Goal: After the domain name is the reference to the file I want to call. So append php on the end of that string but keep the query parameters
With your shown samples, attempts please try following .htaccess rules file. Please make sure to clear your browser cache before testing your URLs. Also make sure that your .htaccess rules file and .php file are residing on same folder.
RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/?]*)/?\?action=(.+)$ $1.php?action=$2 [NC,L,QSA]
One more thing, you have not created a capturing group on left side of RewriteRule hence you can't use them on the right side of it.

.htaccess only rewrite not redirect

I need a htaccess that will make the following URL:
domain.com/index.php?page=contact
Change the link into:
domain.com/contact
so if I'm doing:
Contact
The link will take me to
domain.com/index.php?page=contact
But the link shown in the link area on the browser will be:
domain.com/contact
can some one please help me?
This should work for you, it should be placed on your root folder where your index.php file is located inside a file called .htaccess:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)$ index.php?page=$1 [L]
What the above rule does?
First we check if the file does not exist with:
RewriteCond %{REQUEST_FILENAME} !-f
Then we check if the folder does not exist with:
RewriteCond %{REQUEST_FILENAME} !-d
Then we check the URL is what we are looking for, in your case domain.com/anything which we do with:
^([^/]+)$
The above will collect anything that is after the first / after domain.com, for example:
domain.com/contact
domain.com/about-us
domain.com/home
The right most part of our rule before the square brackets, tells what to redirect and where to:
index.php?page=$1
Which means we want to redirect whatever we collected from the previous rule, above represented as $1 to index.php?page=$1 which using our previous example would mean:
domain.com/index.php?page=contact
domain.com/index.php?page=about-us
domain.com/index.php?page=home
[L] tells we want to stop, the L is a flag that means LAST, not having any redirection flags with it and the URL not being external or a full domain name, means we want to redirect internally.
RewriteBase / means we are on the root folder of your domain.
RewriteEngine On means we want to activate mod_rewrite which is responsible for the rewrite redirects.
-MultiViews is the only relevant Options in this case so I will only explain that, it takes care that when a folder does not exist, it doesn't look for its replacement to be served in its place.

Basic URL rewriting: How to have a css link, img src, etc, ignore it?

I have a website running at localhost/pm and the RewriteBase is correctly set to /pm/. There is a link tag in my document: <link ... href="themes/default/css/default.css">.
When the url is localhost/pm or localhost/pm/foo the CSS works all right. When there are more slashes in the URL, however, like localhost/pm/foo/bar the relative URL if the stylesheet changes to foo/themes/default/css/default.css.
How do I get this to work without having to put some sort of PHP path resolution in the link tag?
# invoke rewrite engine
RewriteEngine On
RewriteBase /pm/
# Protect application and system files from being viewed
RewriteRule ^(?:system)\b.* index.php/$0 [L]
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all other URLs to index.php/URL
RewriteRule .* index.php/$0 [PT]
EDIT:
Basically what I need now is this:
If request contains folder name /themes/ scrap everything that is before /themes/ and rewrite the rest to /pm/themes/...
I tried it like this: RewriteRule ^.*(/themes/.*)$ /pm/themes/$1 but I get an internal server error. Why?
If I do it like this: RewriteRule ^.*(/themes/.*)$ /pm/themes/ (ie. just remove $1 from the end) and use the URL http://localhost/pm/foo/themes/foo/ the resulting physical location is http://localhost/pm/themes which is what is expected too, which in turn means that at least my regex is correct. What am I missing?
The RewriteRule is almost correct
RewriteRule ^.*(/themes/.*)$ /pm/themes/$1
This rewrites http://localhost/pm/foo/themes/default/css/default.css to http://localhost/pm/themes/themes/default/css/default.css, which is one themes too much. Use this instead
RewriteRule /themes/(.*)$ /pm/themes/$1 [L]
But now you have an endless rewrite loop, because /pm/themes/.. is rewritten again and again. To prevent this, you need a RewriteCond excluding /pm/themes
RewriteCond %{REQUEST_URI} !^/pm/themes/
RewriteRule /themes/(.*)$ /pm/themes/$1 [L]
Now the request is rewritten only once and you're done.
You probably need to add the following lines before your RewriteRule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
It will only evaluate your rewrite rule if the requested file or directory doesn't exist.
You should post your .htaccess file so we can offer better advice

mod_rewrite redirect for non-existent URL's

I've got a an old website that I've taken over. New users essentially get a custom page created for them. It was done in a less than fantastic manner. Currently it actually generates a file named for the slug URL created and symbolically links it to a folder called "/main/". Obviously I want to change this. My plan was simply to have it redirect non-existant folders to "/main/" via .htaccess. Currently this is what my .htaccess looks like:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt|index\.htm)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /main/ [L]
However this generates a redirect loop. What am I missing?
Edit
On that note, I realized I should say I want it to maintain the path that's input. For example if I put http://www.mydomain.com/test_person it should maintain that address, but forward everything to the main folder if that makes sense.
You'll want your rule to not rewrite URLs already beginning with main in order to not have it loop, eg:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !^main/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^[^/]+/(.*)$ /main/$1 [L]
[L] doesn't really mean what you would think [L] should mean in the context of a .htaccess file.
You should be able to get the original url requested from the REQUEST_URI environment variable, but a common thing to do is to pass the slug to the target as a GET variable.
RewriteRule ^([^/]+)/(.*)$ /main/$2?user=$1 [QSA,L]
Or pass everything to a single script which interprets the URL and finds the correct content to return:
RewriteRule ^([^/]+)/(.*)$ /main/index.php?user=$1&path=$2 [QSA,L]

How to use .htaccess to make a URL like http://domain.com/query

I want to make
http://domain.com/index.php?query=query
look like
http://domain.com/query
I know I need to use .htaccess, but I have no idea how to approach this.
Something like:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !index.php
RewriteBase /
RewriteRule ^(.+) /index.php?query=$1
Edited:
If you really want your URLs to look like you asked, you should host all your media files (CSS, JS and images) in another virtual host, lets say, http://media.domain.com - because can't tell the difference if "query" matches the name of an existing file on domain.com.
The keyword to search for is RewriteRule.
The Drupal .htaccess file is a good example of mapping /?q=query to /query, but not redirecting things which provide an explicit match - so /files/something.css which is a real file will not be redirected. Here's the relevant snippet from Drupal's .htaccess with ?q= changed to ?query=.
RewriteEngine On
RewriteBase /
# Rewrite URLs of the form 'index.php?query=x'.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?query=$1 [L,QSA]

Resources