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]
Related
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]
I know there's a bajillian number of questions with this title and I've tried them all. What I'm trying to do is redirect
localhost/site/tours/picnics/
to
localhost/site/tours/picnics
but every code I've tried, (this one for instance)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} \s(.+?)/+[?\s]
RewriteRule ^(.+?)/$ /$1 [R=301,L]
redirects me to localhost/picnics for some reason. I don't understand why it's happening. I've even tried RewriteBase /site/ but it didn't make any difference. Can someone point out the problem in this code?
Edit: Here's my complete htaccess file
RewriteEngine On
#RewriteBase /site/
RewriteRule ^destinations/(.*)$ destinations.php?destId=$1 [NC,L]
#RewriteRule ^tours/? tour-listing.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} \s(.+?)/+[?\s]
RewriteRule ^(.+?)/$ /$1 [R=301,L]
RewriteRule ^tours/?(.*)$ tour-listing.php?cat=$1 [NC,L]
RewriteRule ^tour/([0-9a-zA-Z]+)$ tour.php?id=$1 [NC,L]
RewriteRule ^hotel/([0-9a-zA-Z]+)$ hotel-details.php?id=$1 [NC,L]
Change your rule to this:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} \s/+(.+?)/+[?\s]
RewriteRule /$ /%1 [R=301,L,NE]
$1 captures value relative to your current directory only but %1 is being captured from %{THE_REQUEST} that has original and full REQUEST URL.
So I have a been trying a lot of different htaccess codings to clean up my url
but am not even able to simply remove .html without something going wrong
Code Im using:
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)/$ $1.html [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .*[^/]$ %{REQUEST_URI}/ [L,R=301]
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .*[^/]$ %{REQUEST_URI}/ [L,R=301]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)/$ $1.html [L]
CODE Im hoping to clean up:
<area shape="poly" coords="3,612,78,557,118,627,5,708" href="collabwitus.html" />
Little new to htaccess and would love some help!
I am doing everything from file manager via hostgator so framing an answer in a more step by step version using file manager would be great!
Put the following code :
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+(.*?/)?(?:index)?(.*?)\.html[\s?/] [NC]
RewriteRule ^ /%1%2 [R=302,L,NE]
Try this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
This code worked for me:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
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
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]