Change url in htaccess didnt work - .htaccess

I have this url
mywebsite/chat.php?room=somenameHere
I want to change it like:
mywebsite/chat/somenameHere
I have tried that:
Options +SymLinksIfOwnerMatch -MultiViews
## Mod_rewrite in use.
RewriteEngine On
RewriteRule ^videos/([0-9]+)/?$ /details.php?object=1&id=$1 [L,QSA,NC]
RewriteRule ^articles/([0-9]+)/?$ /details.php?object=0&id=$1 [L,QSA,NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^chat/([A-Za-z0-9-]+)/?$ chat.php?room=$1 [NC,L]
But nothing happen , it didnt work , is there something wrong ? i have looked many tutorials which all say same as i done.
Thanks for guidance!
EDIT:
im getting same page without changing anything and no error. getting this:
mywebsite/chat.php?room=somenameHere

You can use these rules:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /mainwebsite/
RewriteCond %{THE_REQUEST} /chat\.php\?room=([^\s&]+) [NC]
RewriteRule ^ chat/%1? [R=301,L,NE]
RewriteRule ^videos/([0-9]+)/?$ details.php?object=1&id=$1 [L,QSA,NC]
RewriteRule ^articles/([0-9]+)/?$ details.php?object=0&id=$1 [L,QSA,NC]
RewriteRule ^chat/([A-Za-z0-9-]+)/?$ chat.php?room=$1 [NC,L,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
EDIT: As per comments below:
RewriteCond %{THE_REQUEST} /(?:index)?(.*?)\.php[\s?] [NC]
RewriteRule ^ %1 [R=301,L,NE]

Related

Redirect to other location

I have the current setup where my file system looks like this
/var/www/blog.example.com/v1/project.php
To access project.php you go to blog.example.com/project, now I've also added a GET which is a title.
Instead of going to blog.example.com/project?title=This Title I want it to be something like blog.example.com/project/This Title
How may I accomplish that setup? Current .htaccess:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^project/(.*)$ v1/project.php?title=$1 [L,QSA]
RewriteCond %{REQUEST_URI} !/(v1)/ [NC]
RewriteRule ^(.*)$ %{ENV:BASE}v1/$1 [L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA,L]
Have it like this:
# turn off MultiViews to avoid conflicts
Options +FollowSymLinks -MultiViews
RewriteEngine On
# handle /project/abcd URI
RewriteRule ^project/([^/]+)/?$ v1/project.php?title=$1 [L,QSA,NC]
# add .php extension intrnally
RewriteCond %{DOCUMENT_ROOT}/v1/$1\.php -f
RewriteRule ^(.+?)/?$ v1/$1.php [L]
# forward everything to v1/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(?!v1/)(.*)$ v1/$1 [L,NC]
Try it like this,
RewriteEngine on
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([\w-]+)$ v1/$1.php
RewriteCond %{REQUEST_FILENAME}.php !-f
RewriteCond %{REQUEST_FILENAME}.php !-d
RewriteRule ^project/([\w-]+)$ v1/project.php?title=$1 [QSA,L]

Multiple rewrite rules and conditions in htaccess file

In my .htaccess file i have already some rewrite conditions and rules, and its working normal.
Now i need to add "http to https redirect" to my htaccess file.
Here is my old .htaccess file:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
RewriteRule ^([a-z0-9_-]+)$ pages/$1.php [L]
RewriteRule ^([a-z0-9_-]+)/([-a-zA-Z0-9_]+)$ pages/$1/$2.php [L]
RewriteRule ^([a-z0-9_-]+)/([-a-zA-Z0-9_]+)/([-a-zA-Z0-9_]+)$ pages/$1/$2/$3.php [L]
I attempted to add below code to file but it doesnt work properly.
For example if i write the url direct to browser its work (only with www). But if click my any backlinks or google search result links it doesnt work.
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}/$1 [NC,R=301,L]
Where do i wrong? Where should i put the code? Any help would be great. Thanks.
your htaccess could look like this:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.yoursite\.com [NC]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L]
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
RewriteRule ^([a-z0-9_-]+)$ pages/$1.php [L]
RewriteRule ^([a-z0-9_-]+)/([-a-zA-Z0-9_]+)$ pages/$1/$2.php [L]
RewriteRule ^([a-z0-9_-]+)/([-a-zA-Z0-9_]+)/([-a-zA-Z0-9_]+)$ pages/$1/$2/$3.php [L]
At least, it works for me, in my own implementation
You can use the following htaccess :
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}/$1 [NC,R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.+)/?$ $1.php [L]
Clear your browser's cache before testing this

Htaccess same rule for 2 files not working

I am trying to create friendly pagination using htaccess file.
But its not working I guess I am using wrong rule for files.
Check out my codes below.
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{THE_REQUEST} \s/+search\.php\?q=([^\s&]+) [NC]
RewriteRule ^ /search/%1/? [R=301,L]
RewriteRule ^search/$ search/%1 [L,R=301,NC]
RewriteRule ^search/(.*)/$ search.php?q=$1 [L,NC]
RewriteRule ^new/(.*)$ new.php?page=$1 [L,NC]
RewriteRule ^(.*)/$ cat.php?id=$1 [NC]
RewriteRule ^(.*)/(.*)/$ cat2.php?id=$1&page=$2 [NC,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ post.php?id=$1 [NC]
Everything is working fine, only cat2.php is not working.
How to fix it?
You need to switch the order around between your 2 cat rules. (.*) matches everything, including slashes, so it will always match whatever cat2 matches. Try just swapping their orders and include L flags:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{THE_REQUEST} \s/+search\.php\?q=([^\s&]+) [NC]
RewriteRule ^ /search/%1/? [R=301,L]
RewriteRule ^search/$ search/%1 [L,R=301,NC]
RewriteRule ^search/(.*)/$ search.php?q=$1 [L,NC]
RewriteRule ^new/(.*)$ new.php?page=$1 [L,NC]
RewriteRule ^(.*)/(.*)/$ cat2.php?id=$1&page=$2 [L,NC,QSA]
RewriteRule ^(.*)/$ cat.php?id=$1 [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ post.php?id=$1 [NC]

.htaccess /subdirectory/ -> /subdirectory/index

I'm sorry if the topic with same question already exists, but I couldn't find it out.
So. My problem is, that I've a webpage in subdirectory; like..
/var/www/example/
Now, what I really need:
when I try, to get on the site from URL like: www.example.com/examplesubdir/ ; dirname($_SERVER['REQUEST_URI']) isn't returning anything.
That's why I want to rewrite it from www.example.com/examplesubdir/ to www.example.com/examplesubdir/index
My current .htaccess file looks like this:
Options -Indexes +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d [OR]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([A-Za-z0-9]{18})_([A-Za-z0-9]{3,4})$ show_file.php?f=$1&t=$2 [L,QSA]
RewriteRule ^([A-Za-z0-9]{18})$ show_file.php?f=$1 [L,QSA]
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Thank you, and appreciate any help.
RewriteCond is only applicable to next RewriteRule only.
Replace your /var/www/.htaccess code with this:
Options -Indexes +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^([A-Za-z0-9]{18})_([A-Za-z0-9]{3,4})$ show_file.php?f=$1&t=$2 [L,QSA]
RewriteRule ^([A-Za-z0-9]{18})$ show_file.php?f=$1 [L,QSA]
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [NC,L]

htaccess works but content doesn't load

When I use index.php?id=this-is-an-article the page loads with the content, when I use /articles/this-is-an-article the page loads without any content, any idea as to how I can resolve this?
.htaccess:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
# force www. in all requests
RewriteCond %{HTTP_HOST} ^mysite\.net [NC]
RewriteRule ^(.*)$ http://www.mysite.net/$1 [L,R=301]
# enable hiding php extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^(.*)\$ $1.php
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^page/(\d+)*$ ./index.php?p=$1
# index.php?id=this-is-an-article => /articles/this-is-an-article
RewriteRule /articles/(.*) index.php?id=$1 [NC,L]
Thanks in advance.
Get rid of the leading slash. It's also best practice to begin a regex like that with ^ and end it with $.
RewriteRule ^articles/(.*)$ index.php?id=$1 [NC,L]

Resources