How can I rewrite everything after the domain name into get if it is not already get?
For example : example.com/blah/blah.blah
will become example.com/?blah/blah.blah
basically all I want to do is add a ? after the first forward slash if there isnt one already.
Thanks!
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* /index.php?$0 [QSA]
But this solution has some issues, #anubhava's is better ;-)
Quite simply use variable %{REQUEST_URI} as a query parameter:
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/index.php$ [NC]
RewriteRule . /index.php?%{REQUEST_URI} [L,QSA]
Another technique is to use the $_GLOBALS['PATH_INFO'] variable which will give you the rest of the path after the script name, so for:
http://example.com/somefile.php/james/fred/blogs.csv?something=value
PATH_INFO would be set to "/james/fred/blogs.csv" and you would still have the possibility of using GET/POST variables separately as modifiers. This can be quite useful, for example if you want to create a .csv file and have it appear to the remote browser client as if it were named "blogs.csv".
Related
I am working on urls of a site. I want to convert the following url into
http://dev.steelogic.com/solution.php?id=metalbuildingproduct
this url
http://dev.steelogic.com/solution/metalbuildingproduct
this is the htaccess code I am using.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)/(.+)$ $1.php?id=$2
I don't know anything about htaccess. usually I just copy paste the text and it works.
the url with ?id= works fine
but when try to use the 2nd url it doesn't have any id.
I have to do this to many other pages as well and the all contain [.php?id=] which I want to replace with [/] slash
Try this rule in your DocumentRoot/.htaccess:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^/]+)/([^/]+)/?$ /$1.php?id=$2 [L,QSA]
Please try this:
<IfModule mod_rewrite.c>
Options +SymLinksIfOwnerMatch
RewriteBase /
RewriteRule ^solution/([^/.]+)/?$ solution.php?id=$1 [PT,L,NC,QSA]
</IfModule>
Because you say that even without this rule, going to http://example.com/solution/asdf loads /solution.php on your server, something else must be working on your url. Because you say you didn't define such a rule, it must be coming from somewhere else. Either a rule is defined somewhere else (main config file) or MultiViews is enabled. If you can't apply anubhava's solution (disabling MultiViews), move your file somewhere else, for example under /public/solution.php, so that url never partly matches an existing file, then use the following rule.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/public/$1.php -f #partly copied from anubhava's answer
RewriteRule ^([^/]+)/([^/]+)/?$ /public/$1.php?id=$2 [L]
from all of the discussion I concluded that its difficult enough to do it with .htaccess as there might b some sort of module working on the urls of site from the hosting and I get both solution and solution.php directed to same page
so I did it in php for now.
as both of the following pages
http://dev.steelogic.com/solution.php?id=metalbuildingproduct
and
http://dev.steelogic.com/solution/metalbuildingproduct
being going to same pages but with the second url I was not getting query string var. so I wrote a PHP code as I knew on each page how many vars I need so it worked for me. although it may not be a good approach but it works
$URI = explode("/", trim($_SERVER['REQUEST_URI']));
$numElems = count($URI);
if ($numElems <= 4) {
$arr = array();
$arr['id'] = $URI[2];
if (!empty($URI[3]))
$arr['id2'] = $URI[3];
}
if any body can help me with .htaccess I will appreciate.
I have the following .htaccess rewrite problem. We have requests entering with multiple subdirectories and an .html file
for example dir1/file.html
or dir1/dir2/file.html
or dir1/dir2/dir3/file.html
or dir1/dir2/dir3/dir4/file.html
what we eventually need is a rewrite rule to
index.html?dir1=$1&dir2=$2&dir3=$3&dir4=$4&file=$5
(where dir2 to dir5 would be empty if path is too short)
Is there any way to do that directly in the .htaccess file, or is it necessary to handle it in php?
I will see if I can test this out, but this should work for the problem as your question states it:
RewriteRule ^/([a-z0-9]+)/?([^/]*)$ /index.php?dir1=$1&$2 [QSA]
RewriteRule ^/([a-z0-9]+)/([a-z0-9]+)/([a-z0-9]+)/([a-z0-9]+)/?$ /index.php?dir1=$1&dir2=$2&dir3=$3&dir4=$4 [L]
RewriteRule ^/([a-z0-9]+)/([a-z0-9]+)/([a-z0-9]+)/?$ /index.php?dir1=$1&dir2=$2&dir3=$3 [L]
RewriteRule ^/([a-z0-9]+)/([a-z0-9]+)/?$ /index.php?dir1=$1&dir2=$2 [L]
RewriteRule ^/([a-z0-9]+)/?$ /index.php?dir1=$1 [L]
I emphasize should because only you can really say what your site structure is & how this cascading ruleset would affect your application.
The regex used is fairly simple:
([a-z0-9]+)
That captures any directory name with letters & characters. If you want to capture—let’s say—underscores and dashes on top of that, it would change to something like:
([a-z0-9_-]+)
The first rewrite rule I have set—^/([a-z0-9]+)/?([^/]*)$—is to capture anything that comes after dir1 just in case there’s data to capture that is not a strict directory structure. You can comment that out if you wish. Just added it since that’s how I like to handle situations that need URL parsing like this.
Also, have you considered adding this to the rewrite rule? Perhaps at the top before the rules cascade in?
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
I would suggest you this
http://domain/dir1-dir2-dir3-dir4-dir5-file.html
rules
RewriteEngine On
RewriteBase /
RewriteRule (.+)-(.+) $1/$2 [N]
you can even make it dynamic by adding these
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1
this is much cleaner.
UPDATE:
well I didn't mean you use the second part but anyways I explain what I meant, it's not wrong that people use QUERY STRING for passing path in URL but it's important how you do that like in this URL
http://domain/index.php?route=dir1/dir2/dir3/dir4/dir5/file.html
as you can see you will grab the path easily with just one GET variable and even handling of this in RewriteRule is much easier like
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?route=$1&%{QUERY_STRING} [L]
which will let you to have a URL like this
http://domain/dir1/dir2/dir3/.../file.html
and with any reason you may change those slash to hyphen or other acceptable chars which become like this
http://domain/dir1-dir2-dir3-dir4-dir5-dir6-dir7-file.html
and as I said it's more easier to pass the current QUERY_STRING which will look like this
http://domain/dir1-dir2-file.html?q=extra_query_value
and result in PHP if you dump $_GET will be:
q => 'extra_query_value'
route => 'dir1/dir2/file.html'
and finally in PHP you may easily explode them to have all folders' name by individual variable.
Hi I want To Ask How Can I convert http://abc.tk/comments.php?post_referrel_id=16 to http://abc.tk/comments.php?post_referrel_id/16 using .htaccess mod_write
means the user see this link
abc.tk/comments.php?post_referrel_id/16
when he clicks a hyperlink having source
abc.tk/comments.php?post_referrel_id=16
you need research, sir.
check this easy tuto
And for your tips, don't use ? in the rewritten url.
here is a quick code to do that (with ? in the rewritten url)
RewriteEngine on
RewriteRule ^comment.php?post_referrel_id/([0-9]+)$ comment.php?post_referrel_id=$1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^comments.php?post_referrel_id/(.*)$ comments.php/post_referrel_id=$1 [L,QSA]
You may have to add a redirect from one form to another. The above code just tells the server that the /16 is the same with =16
use these lines of code :
RewriteEngine on
RewriteCond %{HTTP_HOST} ^abc.tk
RewriteRule ^comments.php?post_referrel_id=([0-9]+)$ comments.php?post_referrel_id/$1 [R=301,L]
this will be redirect comments.php?post_referrel_id=16 to comments.php?post_referrel_id/16
I need to have URLs fitting this criteria:
www.domain.com/abc
rewritten to be:
www.domain.com/index.php?name=abc
I can do it where the last letter(s) are fixed like this:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^(.*).htm$ index.php?name=$1
which means www.domain.com/abc.htm would go to www.domain.com/index.php?name=abc
but I need it to not have the .htm (or anything after the abc)
Can this be done? I've spent some time trying to find the solution but so far without success.
Thanks in anticipation.
Jon
Try this:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+) /index.php?name=$1 [L]
I usually use RewriteRule ^(.*)$ index.php?name=$1
I am not completely sure on your criteria though, this will send everything to the PHP script.
If you only want to rewrite the first part of a path ie. /abc but not abc/def or abc/ I can change it for you.
I've been working on this for a while and have tried a lot of different solutions I've seen on the web and can't seem to get this to work.
I have a site at www.mydomainname.com. The page that I want to handle ALL page requests is www.mydomain.com/index.php. I'd also like to set this up to work for any other domains that I point to this code base (using wildcards would be the way to go for that I think).
So the following URL types (or any other) should automatically go to index.php, while still keeping the original URL structure in the browser address bar:
www.mydomain.com/
mydomain.com/
www.mydomain.com/item/111
www.mydomain.com/item/itemname/anothervariable/value
www.mydomain.com/item/itemname/?variable=value
I'm using PHP 5 and a recent version of Apache with mod_rewrite enabled.
Thanks in advance for any help!
Simple:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_URI} !\.(jpg|gif|ico|png|bmp|css|js)$
RewriteRule .* index.php
You could use the follow RewriteRule
RewriteEngine On
RewriteRule ^(.*)$ /index.php?originalUrl=$1
Untested, but it should work. You will then also have the original URL available in the 'originalUrl' GET parameter for further parsing.
Include this once per .htaccess file:
Options +FollowSymlinks
RewriteEngine on
RewriteRule (.*) index.php
If you need the information from the matched URL you can modify your RewriteRule to match portions of the old URL or just include everything by using the variables $1 and so forth. If for instance you wanted to get the item number passed in quickly to index.php, you could use this rule:
RewriteRule item/(.*)$ index.php?item=$1