I have many articles in foo.com/articles/
like this:
foo.com/articles/banana.php
foo.com/articles/strawberry.php
and want redirect SEO to subdirectory like this:
banana.foo.com
strawberry.foo.com
EDIT
when I type (banana.foo.com) show me (foo.com/articles/banana.php).
just show and not realy redirect
I can not create many subdomain by Cpanel, therefore (banana.foo.com) do not create by Cpanel
put this in your .htaccess file ( don't forget to replace "yourdomain" with your domain name)
Options +FollowSymLinks
Options +SymLinksIfOwnerMatch
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(.*).yourdomain.com$
RewriteRule (.*) yourdomain.com/articles/%1.php [L]
Now when you request
banana.foo.com
The result will come from
foo.com/articles/banana.php
and so on
You might do something like:
if (preg_match('#.*/articles/([a-z0-9\-_]+)\.php#i', $_SERVER['PHP_SELF'], $match)) {
header('Location: http://' . $match[1] . '.foo.com');
exit;
}
Remember to paste this code before any other output or you'll get an error.
Or via .htaccess with mod_rewrite enabled and RewriteEngine ON:
RewriteRule ^.*/articles/([a-z0-9\-_]+)\.php$ http://$1.foo.com [R,L]
EDIT
The reverse would be something like:
if (preg_match('#^http://([a-z0-9\-_]+)\.foo\.com.*$#i', $_SERVER['HTTP_HOST'], $match)) {
header('Location: http://foo.com/articles/' . $match[1] . '.php');
exit;
}
And with .htaccess:
RewriteCond %{HTTP_HOST} ^([a-z0-9\-_]+).foo.com$
RewriteRule .* http://foo.com/articles/%1.php [R,L]
In .htaccess you can do this with the following:
RewriteRule ^foo.com/articles/([a-z0-9-]+).php $1.foo.com [PT,L]
You'll need mod rewrite installed, and you'll need a wild card subdomain set up in your DNS server. http://en.wikipedia.org/wiki/Wildcard_DNS_record
Related
I am using one webserver for hosting two different websites, each having it's own domain and located in it's own subdirectory on the server. Both domains point to the root directory of my server.
This is my file structure:
root/
domain1/
domain2/
In my root directory i am using a small PHP script to determine which URL is coming in and than forward it to corresponding subdirectory.
if (($_SERVER['SERVER_NAME'] == "www.domain1.com" || $_SERVER['SERVER_NAME'] == "domain1.com") ) {
Header( "HTTP/1.1 301 Moved Permanently" );
header("location: http://www.domain1.com/domain1");
}
else if (($_SERVER['SERVER_NAME'] == "www.domain2.com" || $_SERVER['SERVER_NAME'] == "domain2.com") ) {
Header( "HTTP/1.1 301 Moved Permanently" );
header("location: http://www.domain2.com/domain2");
}
Up to here it works totally fine. When I call ww.domain1.com I get forwarded to the corresponding subdirecoty and the domain changes to www.domain1.com/domain1. This is where my question arrises: How can I hide the subdirectory in the URL? I have been struggeling with this for ages, reading guides to mod_rewrite and searching SO but didn't get anny success.
I have tested my server for RewriteEngine On, which works fine, I Just cant get the desired behaviour.
Edit:Here is my htaccess code, located in the subdirectory domain1. The same code is located in directory2, changed to the naming conventions.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?domain1.com$
RewriteRule !^domain1/ domain1%{REQUEST_URI} [L]
I got the idea for this from SO: SO Article
Thanks in advance ;)
you can use apache conditions in htaccess for this. create a file .htaccess with following code
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)\.domain1\.com
RewriteRule ^(.*)$ /domain1/$1 [L,NC,QSA]
RewriteCond %{HTTP_HOST} ^(.*)\.domain2\.com
RewriteRule ^(.*)$ /domain2/$1 [L,NC,QSA]
this will pass all params too, in addition to silently redirecting to sub directory. your url in browser wont show the redirection.
You can use the following code in Root/.htaccess :
RewriteEngine on
#--Rewrite domain1 to /domain1 folder--#
RewriteCond %{HTTP_HOST} ^(www\.)?domain1.com$
RewriteRule ^((?!domain1).*)$ /domain1/$1 [NC,L]
#--Rewrite domain2 to /domain2 folder--#
RewriteCond %{HTTP_HOST} ^(www\.)?domain2.com$
RewriteRule ^((?!domain2).*)$ /domain2/$1 [NC,L]
I want ajax/file address to be redirected to ajax/file.php using .htaccess. I created a .htaccess file like below but that gives a 500 Internal Server Error.
RewriteEngine On
RewriteRule ^ajax/(.*)$ ajax/$1.php [L]
An additional information is that my website is working under a sub-folder. (localhost/myproject) RewriteRule ^ajax/(.*)$ /ajax/$1.php [L] redirects url to (localhost/ajax/file.php) instead of (localhost/myproject/ajax/file.php
Problem is that .* in your regex also matches file.php.
Use your rule like this:
Options -MultiViews
RewriteEngine On
RewriteRule ^ajax/([^./]+)/?$ ajax/$1.php [L]
I'm not sure if this has been answered before but I tried looking for it. Anyways, I'm currently developing a website but I would like to make the actual site content only accessible from my IP address. I then want .htaccess to redirect all other IP address to a separate file on my server. That one file would be called subscribe.php.
I've tried a couple things but nothing provided me with the result I wanted. I know my server allows .htaccess to be used since I've used it to change some other things such as preventing caches.
You can use mod_rewrite to do that. Add the following in your .htaccess file:
Code:
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REMOTE_ADDR} !=123.45.67.89
RewriteRule index.php$ /subscribe.php [R=301,L]
Alternative solution:
<?php $allow = array("123.456.789", "456.789.123", "789.123.456"); //allowed IPs
if(!in_array($_SERVER['REMOTE_ADDR'], $allow) && !in_array($_SERVER["HTTP_X_FORWARDED_FOR"], $allow)) {
header("Location: http://domain.tld/subscribe.php"); //redirect
exit();
} ?>
Hope this helps!
You can use mod_rewrite for the same.
Add following in your .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REMOTE_ADDR} ^80\.40\.20\.[0-9]$ # your ip here
RewriteCond %{REQUEST_URI} !^/subscribe.php
RewriteRule .? /subscribe.php [R,L]
</IfModule>
Just wanted to know if is possible to use mod_rewrite on a single(or more ) sub folder(s).
have a service in a folder that i would like to call like a rest service syntax .
$url = 'http://mysite/subfolder/parem1/parem2/parem3/';
$_SERVER['REQUEST_URI']
$tokens = explode('/', $url);
echo $tokens[sizeof($tokens)-2];
so the question is if I could catch all the different segments in a index.php in the subfolder, by using forexample modrewrite ( by placing a .htaccess with the rules only in this folder ? ) or how is it done ?
regards
If you want to define rewriting rules for a subfolder, take a look at RewriteBase
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /yoursubfolder
RewriteRule ^(.*)$ index.php?$param=$1
</IfModule>
It is possible. In your .htaccess, you could put something like this:
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^([^/\.]+)/?$ index.php?p1=$1
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ index.php?p1=$1&p2=$2
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ index.php?p1=$1&p2=$2&p3=$3
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ index.php?p1=$1&p2=$2&p3=$3&p4=$4
</IfModule>
In your case you could just put the .htaccess file with the index.php in the folder where your service is located.
This way you can fetch (and sanitize) the $_GET array, instead of fetching the whole url.
I want to redirect all to one script e.g. index.php?url=inputurl
With if/else I want to parse url
in index.php run query for url in my custom table
if url is mach: echo "ok"
else do nothing
How should I set .htaccess in root folder of Wordpress?
Example:
URLs in custom_table:
asd
dfg
ghj
If user puts:
www.mysite.com/asd
-> mod_rewrite should output this: www.mysite.com/index.php?url=asd
Else if user puts:
www.mysite.com/zzz
-> do nothing
I think the following .htaccess should solve the problem:
RewriteEngine On
# Redirects everything that is not index.php to index.php
RewriteCond $1 !^index\.php
RewriteRule ^(.*)$ index.php?url=$1 [L,R]
Edit: to not include your folders and files (like /js, /css, etc.) in rewrite, add the following lines before the RewriteRule line (see comments):
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
And in the PHP script:
$url = $_GET['url'];
// the method is_valid should check if the page exists in DB
if (is_valid($url)) {
// do something here
// maybe redirect with header('Location: path')
} else {
// show a not found page (error 404)
}
You want to both be able to read from a database and do nothing if there is not match.
This would require you run code to access db then return back to apache to process and is not possible from .htacccess (though it is from httpd.conf).
The .htaccess solution would be to specify all the "table" entries inline as below.
RewriteEngine on
RewriteBase /
#if asd or dfg or ghj
RewriteCond %{REQUEST_URI} ^/(asd|dfg|ghj) [NC]
RewriteRule . index.php?url=%{REQUEST_URI} [L]