Need help regarding htaccess pretty url - .htaccess

My .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
RewriteRule ^([a-zA-Z0-9-]+)$ index.php?page=$1 [QSA,L]
RewriteRule ^([a-zA-Z0-9-]+)/$ index.php?page=$1 [QSA,L]
I have defined one key in the URL i.e. "page", which is working fine. Now I need to include a 2nd key but the "key" will be different for every page.
For example:
example.com/index.php?page=user?id=john
example.com/index.php?page=product?url=product-url
need to convert them to:
example.com/user/john
example.com/product/product-url
How can I define individual second keys based on page?

You can use:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
RewriteRule ^user/([^/]+)/?$ index.php?page=user&id=$1 [NC,QSA,L]
RewriteRule ^product/([^/]+)/?$ index.php?page=product&url=$1 [NC,QSA,L]
RewriteRule ^([a-zA-Z0-9-]+)/?$ index.php?page=$1 [QSA,L]

Related

Why does echoing htaccess-value end with file-ending?

I have created an htaccess-file to hide file-endings and prettify URLs. Now trying out everything I witness echoing a value from a prettified URL shows the file-ending in the result.
This varied a lot as I wasn't sure where the problem is. So there was ([0-9a-zA-Z]+) for example. I have also tried solutions from StackOverflow.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^events/([^/]+)$ viewevent.php?c=$1 [L,QSA]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
And my viewevent.php-file
<?
echo $_GET["c"];
?>
I used to try different variations, but it's either error or with the file-ending. And the URL I enter is: url/events/bino.
I expect bino, but I get bino.php.
Where is the problem?
Your rules need more reordering otherwise in present state it will redirect http://example.com/events/foo to https://example.com/viewevent.php?c=foo thus exposing your internal handler.
Moreover you need to add .php for files that actually exist in your filesystem as php file.
You may use this code:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^events/([^/]+)/?$ viewevent.php?c=$1 [L,NC,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
The answer was simple. Put RewriteRule ^([^\.]+)$ $1.php [NC,L] at the end of the file, else it's kind of mixing with the other rules.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^events/([^/]+)$ viewevent.php?c=$1 [L,QSA]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteRule ^([^\.]+)$ $1.php [NC,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

How to remove all query string and extension pages to the modified versions

I have this code in my .htaccess file:
RewriteEngine On
RewriteRule \.(css|jpe?g|gif|png|js)$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^authors/([^/]*)$ authors.php?current_page=$1 [NC,L]
RewriteRule ^([^/]*)/([^/]*)$ books.php?tag=$1&current_page=$2 [NC,L]
RewriteRule ^Books-single/([^/]*)/([^/]*)$ book-single.php?tag=$1&id=$2 [NC,L]
RewriteRule ^Books-by/([^/]*)/([^/]*)$ book-by.php?auth=$1&current_page=$2 [NC,L]
RewriteRule ^authors-by/([^/]*)/([^/]*)$ authors-by.php?author-by=$1&current_page=$2 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
when i type in the dunamic url the address bar like localhost:/somefolder/books.php?tag=Love-Books&page=2 this url still works, I wanted all the url with queries to redirect to its static version url.

Remove php extension from url

I have url example.com/lt.php/example and want remove .php that will be example.com/lt/example
I try with htacces, but I think that's impossible
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://moliplante.com/folder/$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://example.com/folder/$1 [R=301,L]
RewriteRule ^([^/.]+)$ $1.php [L]
This might work:
RewriteRule ^(.+)\.php/(.+)$ $1/$2
The first backreference would capture lt from lt.php and the second backreference would have example and php would be added to it.
Try this, hope it will work
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

mod-rewrite force ?

I need to be able to post links that look like "mysite.com/?d=48YSWD96" & go to file path "/d=48YSWD96.php" So, I need "mysite.com/?d=48YSWD96" to go to "mysite.com/d=48YSWD96". I can't start my file name with a "?" because it will just turn into an index page for the root folder and disregard the content in the file. How do I do this? this is my htacces:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
RewriteBase /
Same thing as the query string match from this question (which has the same .htaccess): url construction htaccess
RewriteCond %{QUERY_STRING} (^|&)d=([A-Za-z0-9]+)($|&)
RewriteRule ^$ /d=%2.php [L]

Resources