i am working for a site,which is in php.....i want to rewrite url
e.g www.3idiots.co.in/stories.php?id=17
if i want to rewrite it as
www.3idiots.co.in/stories/17.html
can any one tell me the code for this to write in .htaccess file.?
I'm assuming you're using Apache with mod_rewrite. Something like
RewriteEngine On
RewriteRule ^/stories/([0-9]+)\.html /stories.php?id=$1
should do the trick. Of course you'll need to make sure that RewriteRule is allowed in that directory. See this wiki page for more information.
mod_rewrite can only rewrite/redirect requested URIs and not those that are in your HTML documents. So you should first make sure, that your PHP application is printing the correct URIs, so /stories/17.html instead of /stories.php?id=17.
After that, you can use the rule suggested by José Basilio:
RewriteRule ^stories/([0-9]+)\.html$ stories.php?id=$1
Though redirecting requests of /stories.php?id=17 externally to /stories/17.html and then internally back to /stories.php?id=17 is possible, it’s not good practice as that would result in twice as many requests. But here’s the rule for that:
RewriteCond %{THE_REQUEST} ^GET\ /stories\.php[?\s]
RewriteCond %{QUERY_STRING} ^(([^&]*&)*)id=([0-9]+)&*([^&].*)?$
RewriteRule ^stories\.php$ /stories/%3.html?%1%4 [L,R=301]
Related
How to redirect all urls with .php? to only .php ?
I´m using
RewriteEngine On
RewriteRule ^(.+\.php)/.*$ http://%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?secure/(.*) https://example.org$1 [R,L]
How can i prevent somone using urls that doesn´t exist on my server?
It´s all about blocking everything that comes after .php like ?123456789
Why should it be an issue if someone adds a query string to a URL? Unless your php scripts react to that, of course, but that is something you can control, can't you?
Nevertheless it certainly is possible to drop such query strings if you want to:
RewriteEngine on
RewriteCond %{QUERY_STRING} !^$
RewriteRule \.php$ %{REQUEST_URI} [QSD,R=301,L]
For this to work the apache rewrite module needs to be loaded into the http server, obviously.
It is a good diea to start with a R=302 temporary redirection first. And to only change that to a R=301 permanent redirection once everything works as desired. That prevents nasty caching issues for your users.
You should prefer to implement such rules in the actual http server's host configuration. If you have no access to that you can also use a distributed configuration file (".htaccess"), but that comes with a few disadvantages. And has to be enabled first.
I'd like to make shortened links for my site to be used in Tweets. I'm interested in a t.co-like URLs but confused on how to implement the redirect.
Here's how a link on my site typically looks:
https://mysite.com/item/this-is-a-book-on-toasters
Here's how I'd like the shortened link to look which would redirect to the above link
https://ms.co/Im8y2x
Based on googling how to do this, it looks I need to do a 301 redirect.
I'm using PHP, specifically Codeigniter and I guess there is 2 components: the PHP script and .htaccess.
Here's my .htaccess:
RewriteEngine on
RewriteRule ^/?$ https://mysite.com [L]
RewriteRule ^(.*)$ https://ms.co/$1 [R=301,NC]
The PHP I think I need is in here.
Unfortunately, I can't interpret the answers on this link to make a useful script. Might someone help with this? Also, does my .htaccess look right?
This should be the .htaccess code on your shortlinking website (ms.co):
RewriteEngine On
RewriteRule ^(.*)$ https://mysite.com/in.php?id=$1 [R=301,L]
The in.php should contain the script that decodes the $_GET['id'] (via the short hash decoding methods supplied in the link you supplied), matches it against an ID into your database, and retrieves page that it should redirect to.
By the way, the reason I didn't add a NC part in the code is because upper/lowercase (often) can yield different results when using decoding methods.
I've anaswered my own question. Here's what they .htaccess directive should look like given the question above:
RewriteCond %{HTTP_HOST} ^o-a\.co$ [NC] // the rewrite rule
RewriteCond %{HTTPS} =on //to enable HTTPS
RewriteRule ^(.*)$ https://mysite.com/page-to-handle-hash/$1 [L] //where in your application you want to send the 6-digit hash
I know that there are many questions about this subject, but the questions, and even more the answers are kind of confusing me.
What I want to do:
I want to have an internet page, wich, depending on the URL, shows different content. However, in the backend, all pages are handled by one central PHP page.
In other words:
www.example.com/
www.example.com/AboutUs
www.example.com/Contact
should all be handled by a single .php script, but in such a way that in the browser of the users the URLS are kept intact.
Now, is this done with .htaccess rewriting or not? And how?
.htaccess using Rewrite would be the best approach for this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ /index.php?url=$1 [QSA,L]
</IfModule>
In your index.php you can use the value of $_GET['uri'] or $_SERVER['REQUEST_URI'] to determine which functionality is being requested.
If you only want your controller script to handle requests for files and directories that don't already exist, you can do:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php?url=$1 [QSA,L]
</IfModule>
Yes, you can achieve this by adding mod_rewrite rules to your .htaccess file. Here is an article with more detailed information: http://net.tutsplus.com/tutorials/other/a-deeper-look-at-mod_rewrite-for-apache/.
It may not help your confusion, but it will at least teach you the proper syntax. Basically, mod_rewrite takes the "clean" URL given in the browser, decodes it using a regular expression, then discretely passes the matches from the regular expression as GET variables.
In other words: mod_rewrite takes "example.com/AboutUs", reads the URL, and serves up whatever would be on the page "example.com/index.php?page=AboutUs" without showing users the actual GET-variable-ridden URL.
I'm writing a website that allows people to asses a web page's readability (Flesch-Kincaid Reading Ease, thank kind of thing).
Ideally I'd like the user to be able merely to preceed the target URL with mine (like many mirror sites do), and hey presto they can see the results.
I'm guessing it's got to be done with mod_rewrite, but I'm not sure how to write it, especially given that URLs may contain so much potential junk.
How would I say:
if request is mysite.com/anything-at-all ).
redirect to mysite.com/?site=anything-at-all
Except in cases where the request is for:
just for mysite.com/
The request is for mysite.com/ajaxresponse.php?target=something
Where the request is for about.php or loading.gif
Sadly everything that I have tried so far ends up in an redirect loop...
Many thanks,
Jack
Edit your .htaccess to have:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.+)$ ?site=$1 [NC]
The + in the regex will take care of the index page being left as is. Edit otherwise as you deem necessary (make it a 302 permanent redirect, etc...)
For excluding the specific pages you should add a line:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(ajax\.php|whatever\.gif) - [L]
RewriteRule ^(.+)$ ?site=$1 [NC]
Try this rule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{QUERY_STRING} !(^|&)site=[^&]
RewriteRule .+ /?site=$0 [QSA]
The first condition is to exclude requests to existing files and the second is to avoid a redirect is there already is a site URL argument.
my boss wants me to code an URLrewriting for, let's say "http://our.domain.com/SomeText" -- that is, if the URL does NOT contain ".php" or ".html", it should be processed by a script like "http://our.domain.com/process.php?string=SomeText".
Is this possible with a simple regex? I just cannot find the right expression to do this.
I started by redirecting requests to some special subdir, but now it should work directly without any further subdir, so I somehow have to separate regular requests for exuisting pages to requests that don't contain ".php" or ".html"...
Any advice or link for me?
Best regards,
Roman.
Something like this should do the trick
RewriteEngine on
RewriteCond %{REQUEST_URI} !\.php
RewriteCond %{REQUEST_URI} !\.html
RewriteRule (.*) /process.php?string=$1 [QSA]
there are some caveats regarding if this will go in .htaccess or directly in the VirtualHost definition (to put or not a leading /).
Additionally you should load mod_rewrite.
All this supposing you are actually using Apache and can use mod_rewrite.
What this does is that if the requested URI doesn't contain the strings .php and .html it then internally redirects the requests to the process.php file with what was written as a query string argument.
If you have problems with this then use
RewriteLog "/tmp/rewrite.log"
RewriteLogLevel 9
and check how it is working. For this to work you have to control the server, as it has to be used in a main configuration file (no .htaccess possible).
Using mod_rewite you could do this to re-write .php and .html URLs like this:
RewriteEngine on
RewriteRule !\.(php|html)$ process.php/$1 [NC] [QSA]
However you may also want to stop it from re-writing URLs to files which actually exist. (There may be other files lying around - images/css etc. which you don't want to be re-written) You could amend the above to add checks:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule !\.(php|html)$ process.php/$1 [NC] [QSA]
Adding this check may mean that you don't actually need check whether the extension is anything other than .php or .html, because you know that it will only be rewritten if the requested file does not exist, so you could amend the RewriteRule line to be this:
RewriteRule ^(.*)$ process.php/$1 [NC] [QSA]
This will mean any requested URL that does not exist as a file will be handled by process.php.
why you dont use mod rewrite ?
link to explain :
http://www.workingwith.me.uk/articles/scripting/mod_rewrite
At least with mod_rewrite you can use a regex which matches .php and .html, and then prepend that with a !, which should negate it.