.htaccess link with parameters - php

I have a page with URL like this:
https://something.com/paste/log.php?log=H7nSEIPaVr
(By the way my homepage is:
https://something.com/paste/index.php
)
I want to make it work by just giving the log parameter like this:
https://something.com/paste/H7nSEIPaVr
And it would redirect me to the original url.
(or it would be better if it just give me the paramter - ?log=xxx)
I have a .htaccess file, but it is not working for me.
(rewrite is enabled, so the htaccess is working well)
RewriteEngine on
RewriteRule /paste/^([a-zA-Z0-9]*$)/? /log.php?log=$1 [QSA]

This probably is the rule you are looking for:
RewriteEngine on
RewriteRule ^/?paste/([a-zA-Z0-9]+)/?$ /paste/log.php?log=$1 [L]
That rule needs to be implemented in the central http server's host configuration. Or, if you do not have access to that, you can instead use a distributed configuration file (typically called ".htaccess") which has to be located inside the http server's DOCUMENT_ROOT folder.
An alternative would be to place that variant in a distributed file inside the /paste folder:
RewriteEngine on
RewriteRule ^([a-zA-Z0-9]+)/?$ log.php?log=$1 [L]

Related

short URL through htaccess file in php

I have a website http://sharenotes.xyz/.
In this website users can save and share quick notes to others users.
There is a unique note id for each note. (id can only contain [0-9A-Za-z_] charachters).
Unique note id is present in the url http://sharenotes.xyz/hithere.
In this case hithere is the unique note id.
In actual the url is like
http://sharenotes.xyz/index.php?id=hithere.
My folder structure looks like -
and index.php file is present in public folder.
What will be the content of the .htaccess file to short the url from http://sharenotes.xyz/index.php?id=hithere to http://sharenotes.xyz/hithere and in which folder should I place that .htaccess file ?
I know php but I am new in htaccess file (stored in public_html folder).
UPDATE
I was forget to tell you something that -
There is folder named as public which servers all user accessible files.
So I have also hide the name public from the url throught .htaccess file.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]
</IfModule>
Options -Indexes
That's why you wouldn't see public in url.
This would be the required rewriting rule:
RewriteEngine on
RewriteRule ^/?([0-9a-zA-Z_]+)/?$ /public/index.php?id=$1 [END]
Best is to implement such rules in the actual http server's host configuration. Only if you do not have access to that, then you should use a distributed configuration file, often called ".htaccess". But that comes with a number of disadvantages. If you decide to use one, then place it inside the top folder of your hosts DOCUMENT_ROOT, so here inside the "public_html" folder.
Obviously the rewriting modules needs to be loaded into the apache http server for this. And if you are using a distributed configuration file then you also need to enable the interpretation of such files for that location (read about the AllowOverride directive in the documentation of the http server).
Most likely you will need to add further rewriting rules to sort out requests to other resources.
UPDATE
Considering your comments and the additional information you now added to describe your actual situation this variant probably is close to what you are looking for:
RewriteEngine on
RewriteCond /public%{REQUEST_URI} -f [OR]
RewriteCond /public%{REQUEST_URI} -d
RewriteRule ^ /public%{REQUEST_URI} [END]
RewriteRule ^/?([0-9a-zA-Z_]+)/?$ /public/index.php?id=$1 [END]

HTACCESS: URI rewrite everything to index.php (Within the same directory)

I have a uri: www.domain.com/leasing/properties/
Within that directory, I created a file .htaccess:
RewriteEngine on
RewriteRule ^(.*)$ index.php?uri=$1 [L,QSA]
What I want, is that regardless of that users type after properties/ (Ej. www.domain.com/leasing/properties/somethigelse/I-do-not.care/what##$%they\type) to be handled by www.domain.com/leasing/properties/index.php
I do not need structure or anything else. index.php will grab $_REQUEST['uri'] and check against a database. So, again, I do not care what's typed after www.domain.com/leasing/properties/ so long index.php can get the URI and handle an action according to $_REQUEST['uri']
I want the address bar to not not change. Whatever a user types, to remain the same.
The current file is giving me a 404 error.
PS. I know close to nothing about Apache .htaccess
Create a .htaccess file with the following:
RewriteEngine On
RewriteRule ^([^/]+)(.*)$ /leasing/properties/ [R]
And place it within /leasing/properties/ folder.
RewriteEngine on
RewriteRule ^/leasing/properties/(.*)$ /leasing/properties/index.php [L,QSA]
If that does not work, your Apache server may not be configured to allow .htaccess files.

Redirect subdomain to CakePHP action

Background
I have a CakePHP application that lives in /m/. I want to write a root-level .htaccess file which will redirect "subdomains" for the site as parameters to actions.
For example: I want to write a rewrite rule which will result in redirects like this -
http://mysite.myserver.com → http://myserver.com/m/mysite/
http://mysite.myserver.com/home → http://myserver.com/m/mysite/home
http://mysite.myserver.com/foo/bar?baz=true → http://myserver.com/m/mysite/foo/bar?baz=true
Ideally, this redirect should be invisible to users (I don't want to use a 301, because I don't want to change the URL).
Here's my current attempt:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)\.myserver\.com$ [NC]
RewriteRule ^(.*)$ http://myserver.com/m/%1/$1 [L]
</IfModule>
As far as I can tell, the main issue is with $_SERVER['REQUEST_URI']:
If I browse to http://myserver.com/m/mysite/home directly, $_SERVER['REQUEST_URI'] = /m/mysite/home.
If I browse to http://mysite.myserver.com/home using the .htaccess file above, $_SERVER['REQUEST_URI'] = /home.
The Issue
Because of the issues with $_SERVER['REQUEST_URI'], my routes aren't parsing correctly: it's trying to take the user to /home rather than /m/mysite/home as desired.
How can I change my rewrite rule to make this work properly? Is there another way to accomplish this?
What you're asking (change the domain name in URL but don't let browser see URL change) is not achievable under normal scenario. However to make it possible you have enable mod_proxy in your Apache and restart it. Once that is done use following code in your site root .htaccess:
RewriteCond %{HTTP_HOST} ^([^.]+)\.myserver\.com$ [NC]
RewriteRule !^m/ http://myserver.com/m/sites/%1%{REQUEST_URI} [NC,L,P]

Setup clean URLs like CodeIgniter

I like the way CodeIgniter cleans URLs for sites, however, I have a site that is too involved to start over using the CI framework (at least for now), and I don't need the depth CI provides, I only have one level deep.
Is there an easy way to do this easily using straight PHP?
index.php?id=2454
index.php/2454/
NOTE: I need a straight PHP solution because the server is Windows and .htaccess is not setup to work.
If you use mod_rewrite in apache you can allow your application to dispatch requests as you want.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
Say your server is located # http://coolguy.com and a users accesses http://coolguy.com/mycleanurl/
With the above rewrite rule in your .htaccess or apache configuration you can intercept which url is being accessed via $_SERVER['REDIRECT_URL'] and send it off to the specific code point you want.
The "RewriteCond" directives i have in there are used to ignore this rewrite rule if there exists a file directly at the location the user has specified, this is handy for static assets like CSS and images where you dont want to have to dispatch these requests yourself.
Check out $_SERVER['PATH_INFO'] - it returns anything trailing the script filename but preceding the query.
For example, in the URL:
http://www.domain.com/index.php/var1/var2
$_SERVER['PATH_INFO'] would contain /var1/var2
You could then write a function in your __construct() or init(), etc, to parse the path (e.g. explode("/", $_SERVER['PATH_INFO'])) and use the resulting array as variables.
Are you using Apache? If so, look into files called .htaccess. They rewrite the URL when they're stored in your directory. So if you put an .htaccess file in your web root with
# .htaccess
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/www/
RewriteRule ^(.*)$ /www/$1 [L]
then when a person goes to the URL yourdomain.com/page1, it will actually retrieve the resource yourdomain.com/www/page1 on your server, but their browser won't see the www.
So, for very simple URL rewriting you can use this to hide the ?var=val&var2=val2 crap in the URL
Information on the rules is here
If your using PHP on windows and want to do URL Rewriting you have two choices:
Url Rewrite (which allows you to use rewrite rules in the web.config) http://www.iis.net/download/urlrewrite - Open the site in IIS Manager and select "Url Rewriting", then add your rules.
Buy Helicon-APE or similar (which allows you to use a native .htaccess file) http://www.helicontech.com/ape/ - we use this on our shared windows hosting servers with great success

how to use different url structure in php?

how to use different url structure in php ?
I want to know to handle url like this
www.example.com/10/2011
instead of
www.example.com/?m=10&y=2011
Maybe it will be better to do with server configuration - .htaccess (in Apache) or web.config (in IIS). Create rewrite rule from ^(\d+)/(\d+)$ to /?m=$1&y=$2. For example, in Apache it will look like:
RewriteEngine On
RewriteBase /
RewriteRule ^(\d+)/(\d+)$ /?m=$1&y=$2
this can be achieved through apache's mod_rewrite mechanism. create a .htaccess file in your webroot with the following contents:
RewriteEngine on
RewriteRule ^([0-9]+)/([0-9]+) ?m=$1&y=$2 [NC]
similar mechanisms exist for other web servers.
You should use Rewrite(google it) engine in Apache or nginx.
Something like this exists in ISS too.
This is not achieved by PHP, but by some other "component". E.g. the Apache web server has a module (the rewrite engine) that can rewrite the URL so that you can use URL like www.example.com/10/2011 and it translates them into www.example.com/?m=10&y=2011 so that in your PHP code you can use $_GET['m'] and get the month. Rewrite engine for Apache (for other webserver similar things may exist)
One easy method is something like this:
In your .htaccess file
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L,QSA]
What this does it redirects everyhting to index.php if its not a physical file or dir.
Then you configure your index.php to handle this request.
Example
www.example.com/10/2011
will become $_GET['10/2011'];
so you can use data in your script.

Categories