I am using the following .htaccess code in my website to redirect all the urls to index.php
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
then I am checking the url and see if the name of the page is available in the database. Then I am redirecting the user the that specific page if it is found like so:
www.mywebsite.com/videos.php?v=Name_of_the_video
www.mywebsite.com/images.php?i=Name_of_the_image
www.mywebsite.com/users.php?u=Name_of_the_user
as you can see I have 3 main types of pages. I want to use .htacces so I can convert those urls like so:
www.mywebsite.com/videos/Name_of_the_video
www.mywebsite.com/images/Name_of_the_image
www.mywebsite.com/users/Name_of_the_user
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/])([^/]+)/(.*) /$1$2.php?$1=$3 [L,QSA]
You'll probably want to put this before your other rules though.
Related
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9-/_]+)/?$ index.php?id=$1 [L,QSA]
the above i have in my htacces file which rewrites directory/page to index.php?id=directory/page
thats working fine.
I also want to be able to add the following to it:
domain.com/sections/page rewrites to index.php?section=1&id=page
domain.com/sections/page2 rewrites to index.php?section=1&id=page2
domain.com/page rewrites to index.php?id=page
the ID is going to be different for each page
You have to take a look at RewriteCond and RewriteRule directives.
That's a sample .htaccess based on your edit.
RewriteEngine On
# This will process the /sections/(.*) requests, ?section=1 will be appended to query string
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^sections\/(.*)?$ index.php?section=1&id=$1 [L,QSA]
# This will process the other requests, as it does now.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9-/_]+)/?$ index.php?id=$1 [L,QSA]
I need to simultaneously do two things with htaccess.
I need to take a URL like:
http://client.example.com/123
and rewrite the directory to a param, and simultaneously add another subdomain to the url so it looks like this:
http://client.qa.example.com/?param=123
This does the param bit correctly, but I can't figure out how to add the subdir:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/[^/]+/?$
RewriteRule ^([^/]*)/?$ /?param=$1 [L]
You can examine the host header using a RewriteCond and extract the relevant parts of the name. Use them in the rewrite. Back references to matches in RewriteConds appear as %n
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} (.+?)\.(.*)
RewriteRule ^([^/]*)/?$ http://%1.qa.%2/?param=$1 [R,L]
(.+?)\.(.*) will do a match on everything up to the first . and then everything to the end. So client and example.com will respectively be in %1 and %2
If your .htaccess is in the root of client.example.com, it should be a simple redirect. Of course the directory has to be a fake directory or this won't redirect.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ http://client.qa.example.com/?param=$1 [R=301,QSA,L]
You can use the following to match (check for htaccess syntax):
(http://[^.]+\.)([^/]+/)([^/]*)/?$
And replace with:
$1qa.$2?param=$3
See DEMO
Finally got it working using:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} (.+?)\.(.*)
RewriteRule ^([^/]*)/?$ http://%1.qa.%2/?param=$1 [R,L]
Now I just have to figure out how to work in 2 parameters, given that param 2 isn't always going to be present.
I have such urls:
http://site.ru/ontent/wefwefw/article.php?article=369-tayskaya-kuhnya-recept-s-foto
http://site.ru/ontent/wefwefw/article.php?article=32237-ogurci-recepti-na-zimu
http://site.ru/ontent/wefwefw/article.php?article=90-ogurci-na-zimu-recepti-po-koreyski
I want to rewrite tham like:
http://site.ru/tayskaya-kuhnya-recept-s-foto.html
http://site.ru/ogurci-recepti-na-zimu.html
I tried smth like:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ ontent/wefwefw/$1 [L]
But how cut unnecessary parts of string?
Unless article.php is able to derive the right article from the title alone, what you want to do is not possible. mod_rewrite is good at rewriting things, but it can't summon an article-id from thin air if it isn't in the original request. You would have something like:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)\.html$ ontent/wefwefw/article.php?article=$1 [L]
When you would request http://site.ru/tayskaya-kuhnya-recept-s-foto.html, it will load http://site.ru/ontent/wefwefw/article.php?article=tayskaya-kuhnya-recept-s-foto. Then you have to get the id 369 in some other way based on the title if needed.
The best in this case is:
http://site.ru/369-tayskaya-kuhnya-recept-s-foto.html
redirect to
http://site.ru/ontent/wefwefw/article.php?article=369
with:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(\d+)-[^/]*\.html$ ontent/wefwefw/article.php?article=$1 [L]
This is my folder structure
htdocs
-> testing
->index.php
->.htaccess
Inside index.php I have this code
<?php
if(isset($_SERVER['PATH_INFO'])) {
echo $_SERVER['PATH_INFO'];
}
?>
Inside .htaccess I have this code
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php/$1 [NC,L]
Here is my question:
In the above htaccess rewrite rule, which will link my address localhost/testing/HelloWorld to localhost/testing/index.php/HelloWorld
Is it possible to do some thing like this? When the user enter localhost/testing/index.php/HelloWorld I want the browser to hide the word index.php and only display something like this localhost/testing/HelloWorld
I tried to redirect the link localhost/testing/index.php/HelloWorld to localhost/testing/HelloWorld by adding another rule in .htacces file but I get "The webpage has redirect loop" error message.
After I add in new rule the .htacces file looks like this
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php/$1 [NC,L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^index.php/(.+)$ /testing/$1 [R]
Does any ways that I could make my link like this? Thanks in advanced, your help will be very much appreciated.
You get the redirect loop, because the rewritten url matches the rule for redirection. You'll need to use a trick to make the external redirect only happen on an external request with index.php, not on an internal rewrite that maps a different request to that file. You can do this with %{THE_REQUEST} which will only ever be whatever the external request was. It will not update if you rewrite the file it maps to.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php/$1 [NC,L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /testing/index\.php/
RewriteRule ^index.php/(.+)$ /testing/$1 [R]
I'm trying to do four things with .htaccess
First thing is removing the php extension from all files
so
www.mywebsite.com/home is written www.mywebsite.com/home.php
I want to change to error page so if a request is made to www.mywebsite.com/adakadabra and there is such page like adakabra.php a custom 404.php is used unstead.
Thirdly I would like to have vain url such that users can have profile links like
www.mywebsite.com/Joe
I have a profile.php and I use a get request to get the username so the rewrite would be from
www.mywebsite.com/joe to www.mywebsite.com/profile.php?username=joe
Last but not least
I would want to have a pages like www.mywebsite.com/p/123453
to rewrite to
www.mywebsite.com/p.php?photo=123453
I have the page p.php on my webserver.
This can to it, with 3 sets of rules in /.htaccess:
ErrorDocument 404 /404.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^p/([0-9]+)$ /p.php?photo=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(Joe|Mao|Napoleon|Gandi)$ /profile.php?username=$1 [QSA,L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^.]+)$ /$1.php [QSA,L]
Here how it works:
www.mywebsite.com/p/123453 => /p.php?photo=123453
www.mywebsite.com/Joe => /profile.php?username=Joe
www.mywebsite.com/home => /home.php,
www.mywebsite.com/houra/foo/bar/home => /houra/foo/bar/home.php