ht access issue, with examples - php

My end goal is to have something like this.
127.0.0.1/site/search/search-term
Rather than,
127.0.0.1/site/search.php?term=
I have tried two pieces of code, first being.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ../search.php?term=$1
And i put this inside a folder called /search. The HTACCESS works perfectly but the css gets messed up for my website, it just shows everything as plain text without any styling.
Secondly i tried
RewriteEngine On
RewriteBase /search/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ../search.php?term=$1
And put that in 127.0.0.1/site/ root. But when i tried 127.0.0.1/site/search/searchterm I get a 404.
Can anyone see where i am going wrong? I am guessing the first option is the best one to work towards getting fixed as it is so close, i look through the source of the page i receive after using the vainty url and i think it might be something to do with how im linking the style sheet? It works if i put the full address in for the link (127.0.0.1/site/main.css) But then all it fails on all my jquery includes etc. Is it wise to just put the full address in when ever i want to use vanity urls?

First make sure you're using absolute paths in CSS, JS and images files. That means path to these resources should either start with a slash / or http://. If you don't already have it then please make necessary code changes. Once that is done use following .htaccess in $DOCUMENT_ROOT (not in $DOCUMENT_ROOT/site):
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^site/search/(.+)/?$ site/search.php?term=$1 [L,QSA,NC]
EDIT: As per your comments here is the code to make a URI in lowercase:
First add this line in section OR at the end of your httpd.conf file:
RewriteMap lc int:tolower
Then have your rules like this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^site/search/(.+)/?$ site/search.php?term=${lc:$1] [L,QSA,NC]

Related

.htaccess special rule exception for ajax?

So having very little experience with Regex as well as minor beef with .htaccess I need to ask this:
I want to use AJAX on my site, but my mod rewrite configuration prevents me from doing this properly, as I redirected all urls to my index.php and set the url $_GET variable, this is my current setup:
RewriteEngine On
Options -Indexes
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
Now I want this rule to not fire if the query contains "&ajax&" as a string, at least I always append "&ajax&time=..." to the ajax path that I want to load.
So I thought it would be easiest to check if I can match "ajax" and use the [S]kip or [L]ast Flag to prevent the other rule to fire, but haven't been able to achieve it yet, any help? With explanations or pointers toward some?
Also, it would be neat if this doesn't require absolute urls or anything, as I want it to work in both development (localhost) and live (actual domain) environment without changing the .htaccess file accordingly.
SO
I want the url example.com/profile
to be redirected to example.com/index.php?url=profile
AND
I want the url example.com/src/file.php?stuff=something&ajax&time=1234
to stay example.com/src/file.php?stuff=something&ajax&time=1234
BUT
I want the url example.com/ajax
to also be redirected to example.com/index.php?url=ajax (just in case)
Any help is greatly appreciated, thanks!
You just need to add another RewriteCond in your rule that skips this rule if query string contains ajax:
Options -Indexes
RewriteEngine On
RewriteCond %{QUERY_STRING} !(?:^|&)ajax [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
The htaccess file shouldn't rewrite requests for valid files. This is the line that says so:
RewriteCond %{REQUEST_FILENAME} !-f
So what you want should already be happening, assuming src/file.php exists on the server.

.htaccess to load index.php file for friendly url

I have this url:
www.mywebsite.com/news/best-ever-phones
The part best-ever-phones is variable and could be anything.
index.php file is checking database for article with url best-ever-phones
The problem is that in ./news/.htaccess file I have this code:
RewriteEngine On
RewriteBase /news/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$ index.php?article=$1 [L,QSA]
which is supposed to load index.php file but server returns error 404 Not found when I visit www.mywebsite.com/news/best-ever-phones url.
What is wrong?
Try this
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?article=$1 [L]
I think the problem may be with Rewrite base and your relative path. But this is how i'ld do it.
<IfModule mod_rewrite.c>
Options -Indexes
RewriteEngine On
RewriteRule ^news/([A-Za-z-.]+)/?$ news.php?article=$1 [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L]
</IfModule>
This should fix you up, rename your index to news.php
Perhaps give this a shot:
RewriteEngine On
RewriteBase /news/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-\/]+)\/?$ index.php?article=$1 [L,QSA]
This is presuming that you want the / character to appear in your $article variable, so you can logically separate different parts of the friendly URI if you want to in /news/index.php. Otherwise, single segment friendly URIs like /news-item-1 or whatnot can end with a trailing slash or not.
Really you don't want trailing slashes, as it might result in duplicate content penalties for loading the same page with a different URL (the / counts), so you could either not match \/? outside of the capture group or 301 redirect in index.php to the URL with the trailing slash removed. This way you also have clean query arguments, e.g. /news/something-big?orly=yes instead of /news/something-big/?orly=yes
I am using this site to test the rewrites. Otherwise untested as we don't have your code handy, so no way to tell whether it would work or not in your environment.
This code is used when we click on any url it will hide its extension.
But If you want to goto index.php page then write
echo 'Index Page ';
?>

Keep URL the same, load index.php

My question might be dumb, but I googled it and didn't find an answer...
Let's say I want to access my website in this given url: www.mywebsite.com/something/something_else/?some_query_string=true
(I put the query string because it is going to be there, and I don't know if it makes any difference in the htaccess file)
I want it to keep the URL the same, but load the index file for no matter what URL, which is not in the root of the server.
My server has an "application" folder, where all the code is.
How can I do this?
Thanks!
use htaccess to re-write all request to index.php (except for when a file/dir/link is requested and it exists):
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* index.php [L]
</IfModule>
If you want to use Rewrite to have your requests handled by a file outside the DocumentRoot, then you can combine with an Alias directive.
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* application/index.php [PT]
Alias application /path/to/application
Note the [PT] on the RewriteRule which means 'pass through' - it ensures the rewritten url is passed through to other apache modules which might be interesting in processing it.
This turned out to answer my own question:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ application/index.php?url=$1 [QSA,L]
If the URL doesn't exist, it loads the index.php file inside of the folder "application", and it keeps the URL the same, which was exactly what I needed...
Thanks for the answers!

Redirecting even absolute url with mode rewrite

i am trying to implement pretty url.
Everything i have written in PHP and it works properly. I need to handle every request by index.php.
For example website.com/page/search/question/dogs will be handled by index.
BUT website.com/templates/header.tpl will start downloading the file.
I want to handle by index.php even adress to another file.
In .htaccess i have.
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^.*$ ./index.php
How can i hande even absolute url to file by index.php?
If you want to rewrite URLs for files that exist on the server already, you need to drop the !-F (not a file) and the !-D (not a directory) check. Otherwise you've setup the wrong RewriteConditions for your RewriteRule.
However, there is one caveat, you don't want to rewrite for index.php itself, otherwise it would result in an endless rewrite-loop.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !=^./index\.php$
RewriteRule ^.*$ ./index.php [L]
See as well RewriteCond Directive­Docs.
rmove the conditions that the url must not be a directory or a filename:
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f

.htaccess url rewriting

can anybody please tell me the rewrite rule so I can make a URL like this:
http://www.mysite.com/index.php?page=directory1/page
what I'm trying to do is remove the "index.php?page=" so i get a
"http://www.mysite.com/directory1/page"
This is how Drupal does it; it's very similar to toneplex's example, but it doesn't mess around with extension checking and it adds an extra check on favicon.ico, which many browsers automatically request; this saves an extra hit on your PHP code if you're missing favicon.ico.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^ index.php [L]
I'm assuming you are trying to bootstrap everything via the index.php file. So try this out. Any file or directory that doesn't exist will be force through the index.php file.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule !\.(js|ico|gif|jpg|png|css)$ /index.php

Categories