Htaccess rewrite URL with virtual directory and 2 variables - regex

its my URL...
result.php?team=arsenal&player=ospina
I want to like this
mysite.com/virtualdirectory/arsenal/ospina.html
I tried this code.. cant work.. Not Found
RewriteRule ^(.*)/(.*)/(.*).html$ result.php?team=$2&player=$3 [L]
The requested URL /subfolder/arsenal/ospina.html was not found on this server.
Apache/2.2.8 (Win32) PHP/5.2.6 Server at localhost Port 80
Thanks for helps,
Best regards!!

ok , I assume you want to change the URI from
http://www.example.com/result.php?team=arsenal&player=ospina
to
http://www.example/subdirectory/arsenal/ospina.html
so this is the .htaccess that will do that for you
RewriteEngine on
RewriteCond %{QUERY_STRING} ^team=(.*)\&player=(.*)$
RewriteRule ^(.*)$ http://www.example.com/subdirectory/%1/%2.html [R=301]
you can test it with htaccess tester here
http://htaccess.madewithlove.be/
and some useful links for documentation and learning:
http://httpd.apache.org/docs/2.2/rewrite/intro.html
http://code.tutsplus.com/tutorials/an-in-depth-guide-to-mod_rewrite-for-apache--net-6708
Great Video Tutorials - Understanding Apache and htaccess
hope that helps

In your .htaccess file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?([^\.*]+)/([^\.*]+)/([^\.*]+).html$ result.php?team=$2&player=$3 [L,QSA]

Related

Url rewriting using RewriteCond whrere different url required based on query param

My requirement is
domain.com/src/home.php?page=category&category=recipe
domain.com/src/home.php?page=article&key=myarticle
domain.com/src/home.php
Expected end url
domain.com/src/category/recipe
domain.com/src/article/myarticle
domain.com
I have written one
RewriteRule ^src/([A-Za-z0-9-]+)/?$ src/home.php?page=article&key=$1 [NC,L]
The problem is if one is working then the other is not.
Can anyone please suggest how to make it work with some condition based on "page" query param.
Thanks in advance.
With your shown samples, attempts, please try following htaccess Rules file. Make sure to keep your htaccess file along with src folder(not inside it).
Also please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteBase /src/
##Rules for handling url domain.com/src/category/recipe here.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^src/(category)/([\w-]+)/?$ src/home.php?page=$1&category=$2 [QSA,NC,L]
##Rules for handling urls like: domain.com/src/article/myarticle
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^src/(articile)/([\w-]+)/?$ src/home.php?page=$1&key=$2 [QSA,NC,L]
##Rule for handling domain.com url here.
RewriteRule ^/?$ src/home.php [QSA,L]

htaccess to remove .php .html extension if exists from URL

I have pages like the following
http://www.domain.com/p.php?p=InfoPages&file=about.php
http://www.domain.com/p.php?p=InfoPages&file=contact
http://www.domain.com/p.php?p=InfoPages&file=post.html
I want to change them to
http://www.domain.com/page/about
http://www.domain.com/page/contact
http://www.domain.com/page/post
RewriteRule ^page/([^/]*)$ /p.php?p=InfoPages&file=$1
I am currently using the above code in .htaccess but it doesn't work if there is a .php or .html extension in the url.
I've googled for this but still i can't find a perfect way to solve since not all links have extensions.
please advise. thanks.
Try :
RewriteEngine on
RewriteCond %{THE_REQUEST} /p\.php\?p=([^&]+)&file=([^.]+)\.php|html [NC]
RewriteRule ^ /page/%2? [NC,L,R]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^page/([^/]+)/?$ /p.php?p=InfoPages&file=$2 [NC,QSA,L]

Apache .htaccess "Redirect to non-url" error

I've not used URL rewriting on .htaccess for a while, and now I think I've forgot some of the basis.
This is my server directory structure:
https://www.domain.com/dir/
In which I've put my website, with those files:
index.php
list.php
edit.php
home.php
What I wanted to achieve is this: User writes url like https://www.domain.com/list and .htaccess sends that request to index.php?section=list.
So, this is my current .htaccess:
RewriteEngine On
RewriteBase /dir
Redirect on HTTPS
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
RewriteRule /([a-zA-Z]+)/$ /index.php?section=$1 [NC,L]
Testing it on htaccess tester seems to work as expected, but not on my webserver.
I've got no more .htaccess files in the server root dir, and by viewing server logs I only get Redirect to non-URL errors.
I've also seen this answer but it didn't help me so far.
Have this .htaccess in /dir/ directory:
RewriteEngine On
RewriteBase /dir/
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=302]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$ index.php?section=$1 [QSA,L]
This will support pretty URL as http://domain.com/dir/list

301 redirect www.root.com/subfolder to www.subfolder.com on same server

Hi people at Stackoverflow,
I've honestly searched here and tried stuff but since I'm really not an expert and nothing worked, I'm lost and need your help. I don't post here often because usually I find what I am looking for.
The situation
I have different sites running on the same server. Let's say that one of my websites is called Cats. It runs with CMSMS and resides in a subfolder of my root called cats.com.
In the root of my server there is a htaccess file with this code (generated by the server admin):
RewriteCond %{HTTP_HOST} ^www.cats.com$
RewriteCond %{REQUEST_URI} !^/cats.com/
RewriteRule (.*) /cats.com/$1
RewriteCond %{HTTP_HOST} ^cats.com$
RewriteCond %{REQUEST_URI} !^/cats.com/
RewriteRule (.*) /cats.com/$1
In the cats.com subfolder there is a htaccess file with rewrite code for pretty urls:
# RewriteBase /cats.com
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?page=$1 [QSA]
This works. When I request www.cats.com I see the contents of the /cats.com subfolder in my root but the URL base stays www.cats.com and all the URLS are extremely pretty.
However, due to a setup error (my bad) in my site's config file, the URL listed in Google is
http://www.root.com/cats.com
When I click the URL, I see the correct page but the address bar reads http://www.root.com/cats.com. The pretty URL rewrite works, but I want this url to rewrite to www.cats.com.
What I have tried
I have tried rewriting the Google listed URL with
attempt one
I tried this in both htaccess files, below and above the existing rewrite rules.
RewriteRule ^/cats.com(.*)$ http://www.cats.com [R=301,L]
RewriteRule ^/cats.com/(.*)$ http://www.cats.com/$1 [R=301,L]
Nothing happens. Everything stays the same.
attempt two
Again tried this in both htaccess files, below and above existing rewrite rules
Redirect 301 /cats.com http://cats.com
This results in an infinite loop in all occasions.
I hope my description is clear enough...
If anybody has any idea what might / should work I'd love to hear it.
Thank you for your help!
Change your rules in cats.com subfolder's htaccess file to this:
RewriteEngine On
RewriteBase /cats.com/
RewriteCond %{THE_REQUEST} /+(cats\.com)/(\S+) [NC]
RewriteRule ^ http://%1/%2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?page=$1 [QSA,L]

htaccess redirect to old website

I changed my whole website with a new once, but now I don't want to lose all the traffic that is coming from google. So i changed the old websites folder to www.domain.com/old/ and there are all of my articles.
How can i redirect from htaccess all the error 404 links on the new website to the old website.
Example:
The link request is www.domain.com/article29384.html and it causes error 404, now i want the htaccess to redirect it to www.domain.com/old/article29384.html
Solution: (thanks to anubhava)
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/old/ [NC]
RewriteRule ^(.+?)$ /old/$1 [NC,L,R=301]
Put this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/old/ [NC]
RewriteRule ^(.+?\.html)$ /old/$1 [NC,L,R=301]