htaccess url rewrite - .htaccess

i have a htaccess file which redirectes all the requests to index.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* ./index.php
My question is i would like to make a redirect to folder which doesn't exist and it should not affect the present htaccess redirects to index.php. since it is linked to entire website
for eg
domain.com/search=kannan&id=21
to
domain.com/kannan
I just need a way to allow only this request and everything else goes to index.php, any ideas...

You could use a condition to capture requests that match a specific pattern, like:
RewriteCond %{REQUEST_URI} ^domain.com/search(.+)$
Then do your rewrite. Or: http://www.google.com/search?q=htaccess+query+string+rewrite

i suggest you to pass id through form . Means use Form post method to pass id then you only need to have 'domain.com/kannan' in url and id will passed as post
Use similar type of code as below -
<form method="post" action="domain.com/kannan">
<input type="hidden" value="21" name="id" />
</form>
In this way form will be posted to domain.com/kannan with id = 21 and url will be domain.com/kannan

<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/public/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /public/$1
#RewriteRule ^ index.php [L]
</IfModule>

Related

rewrite subfolder doesn't work

Following rule doesn't work for subfolder rewriting.
RewriteRule ^cat/([0-9a-zA-Z]+) cat.php?id=$1
RewriteRule ^cat/([0-9a-zA-Z]+)/([0-9a-zA-Z]+) cat.php?id=$1&sid=$2
For example With this rule
<?php
$id='News';
$sid='Politics';
?>
..
In next page, while echoing $_GET['sid'], it doesn't work
Notice: Undefined index: sid in ...
But this rule
RewriteRule ^cat/([0-9a-zA-Z]+)/([0-9a-zA-Z]+) cat.php?id=$1&sid=$2
works, if only there is two querystring parameters
..
but it generate ERROR 500 if there is only one parameter
<a href="cat/<?php echo $id?>>..</a>
Try with below,
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^cat/([0-9a-zA-Z]+)$ cat.php?id=$1 [L]
RewriteRule ^cat/([0-9a-zA-Z]+)/([0-9a-zA-Z]+)$ cat.php?id=$1&sid=$2 [L]

Rewrite and redirect with php variables

I am simply trying to rewrite automatically this:
From: mysite.com/channel.php?id=BBC&name=British Broadcasting Company &date=today
To: mysite.com/channel-britishbroadcastingcompany-today.html
I've tried with:
RewriteRule ^channel-(.*)-(.*)\.html$ /channel.php?id=1&name=$2&date=$3 [R]
But nothing happens.
Hope this simplest one will help you out. This will redirect if
1. REQUEST_URI is /channel.php
2. QUERY_STRING matches this pattern id=something&name=something&date=something
Redirect this to /channel-%1-%2.html here
1. %1 will hold value of name parameter
2. %2 will hold value of date parameter
RewriteEngine on
Options -MultiViews
RewriteCond %{REQUEST_URI} ^/channel\.php$
RewriteCond %{QUERY_STRING} id=.*?&name=(.*?)&date=(.*)
RewriteRule .* /channel-%1-%2.html? [R=301]
As per the requirement specified by OP to first redirect url on html page on the basis of some query parameters then rewriting the request on previous page. So the complete code of .htaccess will be like this.
RewriteEngine on
Options -MultiViews
RewriteCond %{REQUEST_URI} ^/channel\.php$
RewriteCond %{QUERY_STRING} id=.*?&name=(.*?)&date=(.*)
RewriteRule .* /channel-%1-%2? [R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/channel\-(.*?)\-(.*?)
RewriteRule .* /channel.php? [L]
Explanation of 2nd part which is added.
1. REQUEST_FILENAME if file does not exist as a file and directory.
2. REQUEST_URI If request_uri starts with such pattern channel-somewords-somewords
then rewrite request on /channel.php
If I understand the problem correctly, You currently have a file channel.php and what You want to achieve is get more "friendly" URLs for SEO and general aesthetics in the browser location bar but still have channel.php handle your requests.
If this is really the case then You need a two-way rewrite.
First, You need to take your original URL and redirect it to a new, pretty version.
Second, You need to rewrite this pretty URI internally and still feed it to channel.php behind the scenes.
RewriteEngine On
RewriteBase /
# This part rewrites channel.php?name=X&date=Y into channel-X-Y.html
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_METHOD} =GET
RewriteCond %{QUERY_STRING} (.*\&)?name=([^&]+)\&date=([^&]+)(?:\&(.*))?
RewriteRule ^channel.php$ channel-%2-%3.html?%1%4 [R,L,NE]
# This part rewrites it back into channel.php but keeps the "friendly" URL visible
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^channel-(.*)-(.*).html$ channel.php?name=$1&date=$2 [L,QSA]
Note that the first rule-set limits the rewrite to method GET - otherwise You will lose any submitted POST data.
It also allows for any other query-string parameters to surround name and date (the rest of query-string parameters will pass-through to .html URI and then will be picked back up by channel.php)
Also note the ENV:REDIRECT_STATUS rule - this is crucial, without that part You'll be stuck in redirect loop.
See
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule ^([a-z0-9-_.]+)/?$ index.php?id=$1 [NC,L]
RewriteRule ^([a-z0-9-_.]+)/([a-z0-9]+)/?$ index.php?id=$1&goto=$2 [NC,L]
What it's going to do is check the index.php and replace to some like, site/dir/index.php to site/dir/namehere than in index.php you can use explode() to separate the values of current url ang get the variables
I am assuming you are asking for rewrite although you are using redirect flag in your current rules, and also assuming BBC to be static in id variable then try with below,
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^channel-([^/]+)-([^/]+).html$ channel.php?id=BBC&name=$1&date=$2 [L]

.htaccess Url Rewrite Hashtag by Value

I'm trying to make a url rewrite to a page, where it should find a hashtag.
RewriteEngine On
RewriteRule ^/posts/hashtag/([a-zA-Z0-9_-]+)$ index.php?url=posts&hashtag=$1
But this way does not seem to work.
e.g. if the value $1 is funny, the output should be domain.com/posts/hashtag/funny but it's not.
Try it like this, when you put url domain.com/posts/hashtag/funny in your browser it will rewritten to index.php?url=posts&hashtag=$1 internally.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^posts/hashtag/([\w-]+)$ index.php?url=posts&hashtag=$1 [QSA,L]

.htaccess Rewrite Rules online and localhost

On this website and on wamp localhost
http://www.aproapetot.ro/page.php?categ_id=1&id=1
http://localhost/aproapetot-backup/page.php?categ_id=1&id=1
I tried to build the .htaccess, but without any success.
This is what I wrote for online website:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^page-([0-9]+).html$ page.php?categ_id=$1
RewriteRule ^page\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
</IfModule>
and this for localhost
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /aproapetot-backup/
RewriteRule index.php index.html
RewriteRule ^page-([0-9]+).html$ page.php?categ_id=$1
RewriteRule ^page\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
</IfModule>
Someone please give a little help.
It's a website with jokes so after .ro/page.php?categ_id=1&id=1 I have a category(1) and subcategory(1).
I want to have like this
.ro/jokes/doctors instead of how is now.
I'm not entirely sure what you want based upon the htaccess content, but what I do understand is that you want /jokes/doctors is rewritten to /pages.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !pages.php [NC]
RewriteRule ^(.*)$ /pages.php [L]
The above code will rewrite all requests to pages.php, as long as:
%{REQUEST_FILENAME} "!-f": The file itself does not exist on the server.
%{REQUEST_FILENAME} !pages.php: The request is not made to pages.php (meaning that it already has been rewritten)
Finally you can use PHP to obtain the parameters using:
/* Get all parameters from the URL */
$_GET['path'] = rawurldecode( $_SERVER['REQUEST_URI'] );
/* Remove double slashes and backslahses and convert all slashes and backslashes to '/' */
$_GET['path'] = preg_replace( array('#[/\\\\]+#', '/(\/\.)+(\/)/'), '/', $_GET['path']);
$_GET['path'][0] should contain "jokes"
$_GET['path'][0] should contain "doctors"
==edit==
Redirecting using htaccess (not the best method)
Redirect 301 /page.php?categ_id=6&id=0 /anecdotes
Redirect 301 /page.php?categ_id=1&id=1 /jokes/doctors
PHP (or other server side script). You can perform a check if the url is in fact having numbers (see $_SERVER['REQUEST_URI']) and ifso, add some header redirection to your page.
<?PHP
header('Location: www.aproapetot.ro/' . $cat . '/' . $subcat );
?>

How do I get the ENTIRE URL as seen in the browser without JS?

I have an application I'm building in ColdFusion, whereby all requests will run through the index.cfm file.
I have a .htaccess file that rewrites the URL. So, for example...if I write:
http://domain.com/hello/goodbye/howdy
The actual request always uses index.cfm like so:
http://domain.com/index.cfm/hello/goodbye/howdy
This all works great, but now I'm stuck with how I can grab everything that is in the URL. Not one of the CGI variables don't seem to output the "/hello/goodbye/howdy" part of the URL.
I have tried cgi.path_info and cgi.query_string etc to no avail...they're just blank.
I need to grab everything that comes after the domain name, and do stuff in CF with it. I know it's possible in JS, but I really need this on the server.
Dumping the CGI scope shows me nothing useful in this regard:
<cfdump var="#cgi#" />
Here's my htaccess file for reference:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.cfm$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.cfm [L]
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} !^([^\.]+)\.domain\.com
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
</IfModule>
Thanks.
EDIT:
As an additional note, I've also tried the underlying Java methods like so:
<cfdump var="#getPageContext().getRequest().getContextPath()#" />
<cfdump var="#getPageContext().getRequest().getRequestURL()#" />
<cfdump var="#getPageContext().getRequest().getQueryString()#" />
To no success :(
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.cfm$ - [L]
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} !^([^\.]+)\.domain\.com
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
#Change exists here:
RewriteRule ^(.*)$ /index.cfm?actualuri=$1 [L,QSA]
</IfModule>
try cgi.query_string now. It should have actualuri=/the/path/sent.
Also, put the rewrite rules in the same order as put above.
Check #CGI.REQUEST_URI# - it's undocumented but works
I hope this is what you are looking for.
<cfset link = "http://" & GetHttpRequestData().headers['host'] & GetHttpRequestData().headers['X-REWRITE-URL'] >
I think the simplest means is to actually look at the CGI.PATH_INFO field.

Resources