I need to detect subdomain in cakephp. I assume this can be done through .htaccess rules but I'm a newbie and not much knowledge with .htaccess
You dont need to use .htaccess I found the solution using regex.
$url = "abc.yourdomain.com";
preg_match('/^(?:www\.)?(?:(.+)\.)?(.+\..+)$/i', $url, $matches);
$subdomain = empty($matches[1])? '' : $matches[1];
you will get abc in the subdomain, for www.yourdomain.com or yourdomain.com that will be empty
Related
Basically, what i want is this:
For example, I have a url that looks like this :
example.com/stories/show.php?id=$id
I want that url to look like this
example.com/stories/$id/$title
And I want the title to look like this:
I love stackoverflow to
I-love-stackoverflow
Seperating each words with a dash.
Am I supposed to pass the title along with the id??
e.g example.com/stories/show.php?id=$id&title=$title
And how do I achieve all this with .htaccess ??
A good example of what I want to do is used by stackoverflow.. Check the url of this question.
You only need some simple rule like in the below example
RewriteEngine on
# rewrite stories/<id>/<title> to show.php
RewriteCond %{REQUEST_URI} stories/([0-9]+)/([a-z\-A-Z]+)
RewriteRule (.*) show.php?id=%1&title=%2 [L]
Place the above in your .htaccess file, which should be located under the stories folder. Of course you also need to make sure so that the rewrite module is enabled on your server. This could be done by running the following commands from the terminal:
sudo a2enmod rewrite
sudo apachectl restart
Now you should be able to rewrite an url like:
http://example.com/stories/666/stack-overflow
to:
http://example.com/stories/show.php?id=666&title=stack-overflow
Then you could create your links in the following manner:
<?php
// php example
$title = "I love stackoverflow";
$id = 666;
?>
link
I think you should read up on how url rewriting really works, here is a couple of links that might be useful:
URL Rewriting for Beginners
URL Rewriting Guide
Everyone who have tried to search through error_log files from large websites got lots of links like these bellow due to people who have screwd up some html in third part sites or blogs...
File does not exist: /var/www/vhosts/mydomain.com/httpdocs/materias/137.html'http://...
File does not exist: /var/www/vhosts/mydomain.com/httpdocs/materias/137.html http://...
File does not exist: /var/www/vhosts/mydomain.com/httpdocs/materias/137.html/mydomain...
The problem is some extra chars after the .html...
Its easy to guess the correct url in each case... we just have to truncate the url after the ".html".
Is it possible with .htaccess to rewrite these problematic urls to the correct syntax?
Just eliminating everything after the .
html? And avoiding messing up with url queries in dynamic urls?
Here's what I would like to do ...
Replace ".html " with ".html#"
Replace ".html'" with ".html#"
Replace ".html/" with ".html#"
As everything after the # will be just ignored...
Any simple way to do that with .htaccess?
Just use a Regex:
RewriteRule ^(.*)\.html(.*)$ $1.html
This RedirectMatch rule should work:
RedirectMatch 301 ^(.+?\.html).+$ $1
I am migrating an ASP web site into WordPress. Currently, I am fixing the 404 errors, from the old website to the new but I have find a URL that looks like the following
http://www.mysite.com/www.customer-web-site.com
and I like to redirect this domain to
http://www.customer-web-site.com
currently I have use the following rewrite rule, but with no luck:
RewriteRule ^(www\.([^\.]*)\.([a-z]{2,3})) http://$1 [R=301,L]
but this, redirect me to
http://index.php/
Can somebody help me ?
Kind regards
And what about $1 in the rewrite rule? Does changing it to $0 fixes this?
$1 to $9 provide access to the grouped parts (in parentheses) of the
pattern, from the RewriteRule which is subject to the current set of
RewriteCond conditions. $0 provides access to the whole string matched
by that pattern.
Apache mod_rewire doc
I want to change the URL of my website using .htaccess file :
http://godofindia.com/Guru+Saint+Mahatma-103/
to
http://godofindia.com/Guru-Saint-Mahatma-103/
I only want the + sign to be replaced by - sign
please help
I don’t think that this is possible using plain .htaccess. If you really need to change your URLs, I would use a small script (i.e. PHP) that handles the replacment:
<?php
$uri = $_SERVER['REQUEST_URI'];
$new_uri = str_replace('+', '-', $uri);
header('Location: ' . $new_uri);
And then, rewrite Requests to that script using a RewriteRule:
RewriteEngine On
RewriteRule ^.*+.*$ rewrite-plus.php
I have found same issue, so i am using "%20" instead of "+" sign.
You can check it out that will work in most of all cases. In many site also if you write "test+play" or "test%20play" both work same
May this will help you
try:
RedirectMatch 301 ^/(.*)\%2[Cc]$ /$1-$2
it's my first question here :)
I got a problem for redirecting URL:
I have old URL like www.domain.com/cgi-bin/category.cgi?type=...
And try to redirect them to www.domain.com on the htaccess
but I still have 404 error...
This is my rule :
RewriteRule ^cgi-bin/(.*)$ http://www.domain.com [R=301,L]
I verified if there are something in the conf about cgi-bin but nothing.
I did a test with "cgi-bin2" and it works...
So what can i do ?
I don't know where you problem come from but why don't you try to write a perl script which will redirect to your base domain url ?
(it can work if you have, for example, just few cgi files previously used).
In your example it seems you want to redirect "category.cgi".
so, in our case, write a "category.cgi" file in your "cgi-bin" folder and write this code inside it :
#!/usr/bin/perl
#
# fixedredir.cgi
use strict;
use warnings;
my $URL = "http://www.yourdomain.com/";
print "Status: 301 Moved\nLocation: $URL\n\n"
Hope it help !