htaccess mod_rewrite - rewrite parameter without the need of any extension - .htaccess

I got this htaccess inside example.com/go/
RewriteEngine On
RewriteRule ^([^/]*)\.html$ /go/?there=$1 [L]
so example.com/go/ttt.html becomes example.com/go/?there=ttt
But I don't want any extension, I want to remove .html so I tried:
RewriteEngine On
RewriteRule ^([^/]*)$ /go/?there=$1 [L]
But it's not working, example.com/go/ttt gives Internal Server Error
How to do it right?

Try to add this line :
RewriteCond %{REQUEST_FILENAME}.html -f
So , code will be :
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^([^/]*)$ /go/?there=$1 [L]
Then test example.com/go/ttt
UPDTAE :
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ /go/$1.html [L]
RewriteRule ^([^/]*)\.html$ /go/?there=$1 [L]

Related

Removing index.php and another page inside the root folder from URL with .htaccess

In my Root folder I have index.php and profile.php.
Currently I'm using these rules for rewrite my index.php
from example.com/index.php?a=profile&u=user1 to example.com/profile&u=user1
RewriteEngine On
RewriteCond %{request_filename} -f
RewriteRule ^(.*) $1 [L]
RewriteRule ^(([^/]*)+)(/([^/]{0,32})(/.+)?)?$ index.php?a=$1&q=$3 [L]
RewriteRule ^welcome/([^/]+)/?$ index.php?a=welcome [NC]
RewriteRule ^page/([^/]+)/?$ index.php?a=page&filter=$1 [NC]
It works fine but now I want to rewrite my URL for profile.php page too, like this:
from example.com/profile.php?u=user1 to example.com/user1
How can I edit my .htaccess to rewrite profile.php together?
Based on question and comments above here are the rules that should work for you:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^profile/([^/]+)/?$ profile.php?u=$1 [NC,QSA,L]
RewriteRule ^welcome/([^/]+)/?$ index.php?a=welcome&filter=$1 [NC,QSA,L]
RewriteRule ^page/([^/]+)/?$ index.php?a=page&filter=$1 [NC,QSA,L]
RewriteRule ^(([^/]+)+)(/([^/]{0,32})(/.+)?)?$ index.php?a=$1&q=$3 [L,QSA]

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

SEF Links with mod_rewrite error 500

Hey i want to rewrite my links
mydomain.com/?page=pageName
to
mydomain.com/pageName
I tried with
RewriteEngine On
RewriteRule ^([^/]*)$ /?page=$1 [L]
but it seems to to give an error 500
You have an infinite loop because of your rule.
You must add a condition to avoid it
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/(index\.php)?$
RewriteRule ^([^/]*)$ /?page=$1 [L]
EDIT: taking your comments into consideration
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_URI} ^/(index\.php)?$
RewriteRule ^ - [L]
RewriteRule ^([^/]*)/([^/]*)$ /?page=$1&id=$2 [L]
RewriteRule ^([^/]*)$ /?page=$1 [L]

Replacing link with .htaccess

I am trying to replace link but ending with internal server error
www.domain.com/profile.php?id=1
i want it to ->
www.domain.com/1
.htaccess
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.domain\.com$
RewriteRule ^login\.php\/?(.*)$ "http\:\/\/domain\.com\/$1" [R=301,L]
RewriteRule ^([^/]*)$ /profile.php?id=$1 [L]
You just have to use this code :
RewriteEngine on
RewriteRule ^([0-9]+)$ /profile.php?id=$1 [L]

Delete ugly ?pg=1 from URL via .htacess RewriteRule

I have pagination plugin in my CMS which produces ?pg=1 links. I want to redirect this url without this ugly postfix because without it CMS shows first page too.
So, i have url like http://site.ru/category/schock-crane/?pg=1 or http://site.ru/moyka/?type=granit&pg=1
I want to redirect such urls to http://site.ru/category/schock-crane/ and http://site.ru/moyka/?type=granit respectly.
I tried this .htaccess code
RewriteRule (.*)(&|\?)pg=1$ $1 [R=301,L]
I tried this regexp code at regexp trainers - it worked. But at live server no redirect happens.
Here is whole .htaccess file:
AddDefaultCharset Off
#DirectoryIndex index.php index.html
Options +FollowSymLinks
Options -Indexes
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# fix /?pg=1
RewriteRule (.*)(&|\?)pg=1$ $1 [R=301,L]
# fix .palitra-tab
RewriteCond %{REQUEST_URI} -tab$
RewriteRule (.*)/([0-9]*)/\.(.*)-tab?$ http://site.ru/redirect.php?id=$2 [R=301,L]
RewriteCond %{REQUEST_URI} ^/csss/.* [NC]
RewriteRule csss/(.*)\.css$ css.php?n=$1 [L]
#RewriteRule ^(.*)/csss/(.*) /test.php [L]
RewriteRule ^(.*)/base([0-9]+)/?$ $1?base=$2
#RewriteCond %{REQUEST_FILENAME} -f
#RewriteRule \.(gif|jpeg|jpg|png)$ /images/watermark/index.php [L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+) - [PT,L]
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*) index.php
RewriteCond %{HTTP:Authorization} !^$
RewriteRule .* - [E=REMOTE_USER:%{HTTP:Authorization}]
</IfModule>
You need to use RewriteCond to examine the query string:
RewriteCond %{QUERY_STRING} ^(([^&]*&)*)pg=1(&(.*)|$)
RewriteCond %1%4 (.*?)&?$
RewriteRule ^ %{REQUEST_URI}?%1 [R=301,L]
The second RewriteCond is just to remove a trailing &.
Maybe the missing ^ or / kills the redirect
# fix /?pg=1
RewriteRule ^(.*)(&|\?)pg=1$ /$1 [R=301,L]
or try
RewriteRule ^(.*)(&|\?)pg=1$ http://%{HTTP_HOST}/$1 [R=301,L]

Resources