htaccess RewriteRule removing extension not working - php

Please help me address my problem, the RewriteRuel 1, 2 and 3 is working. The 4th RewriteRule is not working it sends back 404 error.
1. RewriteRule pahina-(.*)-(.*)-(.*)$ pahina.php?page_id=$1&page_title=$2&user_name=$3 [L]
2. RewriteRule pahina-(.*)-(.*)$ pahina.php?page_id=$1&page_title=$2 [L]
3. RewriteRule pahina-(.*)$ pahina.php?user_name=$1 [L]
4. RewriteRule ^pahina$ pahina.php [L]
The 4th RewriteRule is not working, what I want is to change pahina.php to pahina only without extension... Please help me...
Thanks and best regards...

Here is the code used to remove the .php extension
# Remove .php-extension from url
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^\.]+)/$ $1.php
Add this in .htaccess file. This will remove all .php extension.
Specify your file name there

Make sure your patterns are using anchors to avoid matching unwanted text:
Options -MultiViews
RewriteEngine On
RewriteRule ^pahina-([^_]*)-([^_]*)-(.*)$ pahina.php?page_id=$1&page_title=$2&user_name=$3 [L,QSA]
RewriteRule ^pahina-([^_]*)-(.*)$ pahina.php?page_id=$1&page_title=$2 [L,QSA]
RewriteRule ^pahina-(.+)$ pahina.php?user_name=$1 [L,QSA]
RewriteRule ^pahina$ pahina.php [L,QSA]

Related

want to remove two .php extension from url using .htaccess

I want to remove 2 .php extention from my url and redirect it to non .php extention.
I want to redirect my url
http://localhost/pureherb/products.php/rss_feed.php
to
http://localhost/pureherb/products/rss_feed
my code:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^index\.php$ http://localhost/pureherb/ [NC,R]
RewriteRule ^know-your-body-type\.php$ http://localhost/pureherb/know-your-body-type [NC,R]
RewriteRule ^health-care\.php$ http://localhost/pureherb/health-care [NC,R]
RewriteRule ^products\.php$ http://localhost/pureherb/products [NC,R]
</IfModule>
please help me get resolve this issue
It is possible to achieve your requirement by using capture groups in RewriteRule. Here is the example
RewriteRule ^products\.php/rss_feed\.php$ http://localhost/pureherb/products/rss_feed [NC,R]
And here is another example (without hardcoding producs and rss_feed):
RewriteRule ^([^\.]+)\.php/([^\.]+)\.php$ http://localhost/pureherb/$1/$2 [NC,R]

.htaccess remove php extension with pretty url of multiple parameter

I have modified my .htaccess file right now to remove the .php extension to my website. I also have a rule to have a pretty url. My problem however is I'm not sure how to add a second parameter. In addition I wanted to know if it was a problem for the rules if these parameter aren't set.
For example as of now my rules will change mywebsite.com/index.php to mywebsite.com and mywebsite.com/login.php to mywebsite.com/login and mywebsite.com/index.php?week=1 to mywebsite.com/1.
However I have now added a new parameter and my url will now be mywebsite.com/index.php?page=a?week=1 and I would like it to be change to mywebsite.com/a/1 However the "week" or the "page" parameter arent always going to be set. So it could sometimes just be index.php?week=1 or index.php?page=a or they could both be set.
Here are my current rules:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
RewriteRule ^([0-9]+)$ index.php?week=$1
RewriteRule ^([0-9]+)/$ index.php?week=$1
Is what Im asking for even possible?
You can also try this
RewriteEngine on
For week :
RewriteRule ^index/(\d+)$ index.php?week=$1 [NC,L]
For page:
RewriteRule ^index/(\w+)$ index.php?page=$1 [NC,L]
For both week and page:
RewriteRule ^index/(\d+)/(\w+)$ index.php?week=$1&page=$2 [NC,L]
From your answer to my question (see comments), page is letters only while week is digits only, which makes them distinguishable. As a consequence, you do not have to modify your URL scheme by adding an upper-level to distinguish both cases.
That said, a working solution for your problem would be:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ /$1.php [L]
# /digit/ for "week" only
RewriteRule ^([0-9]+)/?$ /index.php?week=$1 [L]
# /alpha/ for "page" only
RewriteRule ^([a-z]+)/?$ /index.php?page=$1 [NC,L]
# mix of both, order is /page/week/
RewriteRule ^([a-z]+)/([0-9]+)/?$ /index.php?page=$1&week=$2 [NC,L]

Multible RewriteRules result in Internal Server Error

forcing this to work makes my kind of crazy so i hope you can help.
I use Rewrite Rules and .htaccess to make my dynamic URL
example.com/page.php?id=1
look like this
example.com/1
using
RewriteRule ^([A-Za-z0-9-]+)/?$ page.php?id=$1 [NC,L]
, and it works perfectly fine so far.
But i also want to hide the filetype in the URL ( impressum.php to impressum) using
RewriteRule (.*) $1.php [L]
So both Rules are working completely correct as long as i dont use them both at the same time. When i do so, which looks like this (my complete file)
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) $1.php [L]
RewriteRule ^([A-Za-z0-9-]+)/?$ page.php?id=$1 [NC,L]
,i get an Internal Server Error. I tried different versions, for example change the positions and so on, but i allways get this error.
So my question is: how do i get both rules together and working, while the URL ending is still hidden and the example.com/1 works too?
Thank you very much for any answer
You can use the following in your .htaccess file:
RewriteEngine on
# Check if the PHP file exists and route accordingly
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule (.*) $1.php [L]
# If not, pass the request to page.php if it contains A-Za-z0-9-
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-z0-9-]+)/?$ page.php?id=$1 [NC,L]
You need two separate rules. Rewrite conditions will only get applied to the immediately following rule and with your php extension rule, you must check that the php file exists before adding the php to the end:
RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*)$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([A-Za-z0-9-]+)/?$ page.php?id=$1 [NC,L]

How url works with or without trailing slash using htaccess

I have htaccess in my PHP website. I want to work url with or without trailing slash.
Below code works only with the trailing slash.
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteRule ^(.*)/$ $1.php [NC,L]
e.g. www.mydomain.com/index/ --Works
www.mydomain.com/index --Not Work
If anybody know how can I get this without trailing slash?
Thanks
You have this rule:
RewriteRule ^(.*)/$ $1.php [NC,L]
If you want to match it regardless of / in the end use:
ErrorDocument 404 /not-found.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ /$1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f will stop reapplication of same rule since $1.php will be a real/valid file.
Put a ? after the / to make it optional.
Fix would be
RewriteRule ^(.*)/?$ $1.php [NC,L]
This is what I did with something similar
RewriteRule ^(.*)/?$ /$1.php [NC,L]

Adding a trailing slash to URL using Apache

I'm working in a directory called testsite and I want all .php extensions to be replaced by a trailing slash. I'm halfway there, in that (for example) entering http://mydomain.com/testsite/about means that http://mydomain.com/testsite/about.php is loaded.
However, I now want the URL to be displayed as ./about/ so that only one version shows up in search engine rankings.
Here's my .htaccess:
RewriteEngine On
RewriteBase /testsite/
RewriteRule ^()$ index.php [NC,L]
RewriteCond %{REQUEST_URI} !(^/?.*\..*$) [NC]
RewriteRule (.*)$ $1.php [NC]
Also, is it possible to preserve (and hide from display any parameters that I pass)?
All help is much appreciated!
Try these rules:
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule .*[^/]$ %{REQUEST_URI}/ [L,R=301]
RewriteRule ^$ index.php [L]
RewriteRule (.*)/$ $1.php [NC]
Try replacing the last rule with:
RewriteRule (.*)$ $1.php [NC] [E=ORIGINAL_URL:$1]

Categories