How to rewrite the website URL in .htaccess - .htaccess

is there a way to replace an url with the .htaccess like this:
http://website.com/file.php?watch=video
to this one:
http://website.com/file/watch/video
Tried with this,and haven't worked.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/watch/([\d]+)$ $1.php?watch=$2 [L]
My .htaccess file:
#remove php file extension-e.g. https://example.com/file.php will become
https://example.com/file
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/watch/([\w-]+)$ $1.php?watch=$2 [L]

Have it this way:
RewriteEngine on
RewriteBase /test/
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /([\w-]+)\.php\?watch=([^\s&]+)\s [NC]
RewriteRule ^ %1/watch/%2? [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/watch/([\w-]+)$ $1.php?watch=$2 [L,QSA,NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

Related

how to rewrite GET param using htaccess

I need to rewrite the url:
https://pieskowato.pl/posts.php?post=param
to
https://pieskowato.pl/posts/param
i found the htaccess config and it works at localhost but not in production..
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{DOCUMENT_ROOT}/$1 -f
RewriteRule ^[^/]+/([^.]+\.(?:js|css|jpe?g|png|gif))$ /$1 [L,R=301,NC]
RewriteRule ^/post/([^/]*)$ /post.php?post=$1 [L]
url https://pieskowato.pl/post/dlaczego-warto-adoptowac-psa
Hosting OVH.com
With your shown samples, could you please try following.
Please make sure to clear your browser cache before testing your URLs.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{DOCUMENT_ROOT}/$1 -f
RewriteRule ^[^/]+/([^.]+\.(?:js|css|jpe?g|png|gif))$ /$1 [L,R=301,NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{DOCUMENT_ROOT}/$1 -f
RewriteRule ^(post)/([^/]*)$ /$1.php?post=$2 [NC,L]
Have it this way:
# important to turn off MultiViews
Options -MultiViews
RewriteEngine On
# fix css/js/images
RewriteCond %{DOCUMENT_ROOT}/$1 -f
RewriteRule ^[^/]+/([^.]+\.(?:js|css|jpe?g|png|gif))$ /$1 [L,R=301,NC]
# handle /post/anything
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^post/([^/]+)/?$ post.php?post=$1 [L,QSA,NC]

Rewrite rules for page and user profile

I can't figure out how to rewrite this URL
http://localhost/site/page.php?id=[pagetitle]
to
http://localhost/site/page/title
and
for user profiles
http://localhost/site/profile.php?username=username
to
http://localhost/site/profile/username
This is the code I'm using:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^profile(.*)$ profile.php?username=$1
RewriteRule ^(.*)$ page.php?title=$1
this will rewrite the input localhost/site/page/title to loacalhost/site/page.php?id=[pagetitle]
RewriteRule ^localhost/site/(.+)$ site/page.php?id=$1
RewriteRule ^localhost/site/profile/(.+)$ site/profile.php?username=$1
This should work
RewriteRule ^([a-zA-Z0-9_-]+)$ profile.php?username=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ profile.php?username=$1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^profile/(.*)$ profile.php?username=$1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^page/(.*)$ page.php?title=$1
assumed that the .htaccess at the /site/ directory

Rewrite rules for removing the `.html` file name extension

I have tried following command in the the .htaccess file to remove .html extension and put slash at the end. But it is not working. Can anyone suggest me what is going wrong?
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.html
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
Use these rules in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+(.+?)\.html[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.html -f
RewriteRule ^(.+?)/?$ /$1.html [L]

How to rewrite rules for two subfolders using mod_rewrite

I would need to do something like this:
########## SITE 1
RewriteEngine on
RewriteBase /mysite1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [L,QSA]
########## SITE 2
RewriteEngine on
RewriteBase /mySecondSite
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [L,QSA]
The problem is that i can only use one .htaccess file as I am using Helicon ISAPI_Rewrite 3 over windows 2003 Server.
Is there a way to combine both .htaccess files in just one of them and making them work properly?
I have tried this just to test if mysite would work without the RewriteBase, but seems not to work:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^mysite1/(.*)$ index.php?url=$1 [L,QSA]
Thanks.
than I can't come up with anything better than this:
########## SITE 1
RewriteEngine on
RewriteCond %{HTTP:Host} ^www\.website\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /mysite1/index.php?url=$1 [L,QSA]
########## SITE 2
RewriteEngine on
RewriteCond %{HTTP:Host} ^www\.demo\.website\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /mySecondSite/index.php?url=$1 [L,QSA]
In case the names of your folders are the same as the names of the sites:
RewriteEngine on
RewriteCond %{HTTP:Host} ^www\.([^.]+)\.com$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /%1/index.php?url=$1 [L,QSA]
Finally I did it like this:
RewriteEngine on
RewriteBase /
########## SITE 1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^site1/(.*)$ site1/index.php?url=$1 [L, QSA]
########## SITE 2
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^site2/(.*)$ site2/index.php?url=$1 [L, QSA]
########## SITE 3 (by URL)
RewriteCond %{HTTP_HOST} ^mysite3.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.mysite3.com$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [L, QSA]

Htaccess Problem in PHP

How can I rewrite
www.example.com/test.php?search=demo
to
www.example.com/test/?search=demo
using htaccess
Put that into your .htaccess:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([A-Za-z0-9]+)/?$ $1.php
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

Resources