I have a URL that is being generated using GET variables from a form in PHP. The issue that I have is that there are two variables being passed. Here is an example URL to clarify:
http://www.examplesite.com/example.php?first=one&second=two
I would like to use a mod_rewrite in my .htaccess to make this a smaller URL. Ideally I would like the URL...
http://www.examplesite.com/one
to be able to redirect to the full URL...
http://www.examplesite.com/example.php?first=one&second=two
but as you can see there are two variables. Is this possible? If not, what is the shortest that I can get the URL using both variables?
Here is my current attempt to solve this with a mod_rewrite
RewriteEngine on
# don't rewrite if the file exists
RewriteCond %{REQUEST_FILENAME} !-f
# don't rewrite if the directory exists
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ example.php?first=$1&second=$2
Try this:
RewriteEngine on
# don't rewrite if the file exists
RewriteCond %{REQUEST_FILENAME} !-f
# don't rewrite if the directory exists
RewriteCond %{REQUEST_FILENAME} !-d
# http://www.examplesite.com/one/two
RewriteRule ^([^/]*)/([^/]*)$ /example.php?first=$1&second=$2 [L]
Related
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.
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 ';
?>
This is my first question:
I have created a URL Shortner Service in PHP.It works completely without any problem but there was a problem:
Someone who wants to access his url should type : MyDomain.com/go.php?u=key
Here i tried to remove .php extention by configuring apache and worked.Now it is like that:
MyDomain.com/go?u=key
but some services such TinyUrl.com works like that: TinyURL.com/key !!!!
How can i get this in php?
thanks a lot.
You basicly use mod_rewrite.
With mod_rewrite you can say that all requests which are like
www.example.com/[A-Za-z1-9]
are redirected to:
www.example.com/shorturl.php?key=$1
While $1 is the extracted variable from the requested URL.
There is no proper way to do it with pure PHP.
The rewrite rule could look like this:
RewriteRule ^([A-Za-z1-9]*)$ shorturl.php?key=$1 [L]
I would exclude files which really exists from the rewriting, use for this
RewriteCond.
This could be done like here:
# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
# If the request is not for a valid link
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([A-Za-z1-9]*)$ shorturl.php?key=$1 [L]
(Source: anubhava at RewriteCond to skip rule if file or directory exists)
create .htaccess file to redirect any request to your domain to one file lets say go.php
so in .htacess do like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ go.php?key=$1 [PT,L]
</IfModule>
This redirection any request like mydomain.com/userkey to mydomain.com/go.php?key=userkey
now in your index.php you can do your redirection login.
<?php
$key = $_GET['key'];
// your logic here.
?>
This ref will solve your problem.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9]+)/?$ redirect.php?c=$1 [L]
</IfModule>
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]
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 DirectiveDocs.
rmove the conditions that the url must not be a directory or a filename:
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f