.htaccess remove php extension with pretty url of multiple parameter - php

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]

Related

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]

PHP query strings to be rewritten with htaccess RewriteCond

I have a switch on index.php, which listens to ?action=
I would like to rewrite anything after index.php/ to index.php?action=
Currently I have a rule to remove index.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]
So a current url looks like;
localhost:8888/?action=viewBasket
and would like to rewrite it to;
localhost:8888/viewBasket
Im getting confused as to wether I need to create a condition for each switch param, so to prevent images, script, sources etc from being rewritten. Or if there is a condition to check if exisits first. Cheers!
I have worked it out. Will remove my question tomorrow if no one provides a better soloution.
# redirect to any directories
RewriteRule ^styles/(.*) styles/$1 [L,QSA]
RewriteRule ^styles/fonts/(.*) styles/fonts/$1 [L,QSA]
RewriteRule ^images/(.*) images/$1 [L,QSA]
RewriteRule ^bower_components/(.*) bower_components/$1 [L,QSA]
RewriteRule ^scripts/(.*) scripts/$1 [L,QSA]
#if not a directory listed above, rewrite the request to ?action=
RewriteRule ^(.*)$ index.php?action=$1 [L,QSA]
Try this out :
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?action=$1 [L,QSA]
Try this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?action=$1 [L]
First line: Added this because I was scratching my head why it was not working for me. I'm sure you know you need this, just a note for others who might view this.
Second line: Checks if it is not a file.
Third line: Checks if it is not a directory. If it's a file or a directory, it will not process your RewriteRules. By default RewriteConds are operated with and.
Fourth line: I'm sure somebody can write a better rule. [L] last rule, stop evaluating RewriteRules. Tells it to rewrite to index.php?action={whatever} without changing the location header.
The gist of the fourth line is this: if your index.php is
var_dump($_GET);
www.example.com/foo
Will yield array(1) { ["action"]=> string(3) "foo" }

URL Rewriting with multiple rules: wrong $_GET variable

I'm very new to URL rewriting. What I'd like to achieve is to have 2 main rules:
Hide all .php extensions
Rewrite domain.com/p.php?id=1 to domain.com/p/1
Here's what I have so far:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^p/(.+)$ p.php?id=$1 [L]
This seemed to work at first: all .php extensions are hidden, and typing domain.com/p/1 displayed domain.com/p.php?id=1.
However, I've just realized that the PHP GET method on this page picks up a wrong value: whereas I want it to pick up 1, it actually picks up 1.php/1. I didn't notice it at first because the database query based on $_GET actually works, which seems odd to me now that I know the value is wrong.
How could I make the combination of these two rewrite rules work as intended?
Thank you!
Place this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+p\.php\?id=([0-9]+) [NC]
RewriteRule ^ /p/%1? [R=301,L]
RewriteRule ^p/([0-9]+)/?$ /p.php?id=$1 [L,QSA,NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]
If you just want to rewrite the numerical values, you should restrict your second rule:
RewriteRule ^p/(\d+)$ p.php?id=$1 [L]

url rewrite not working .htaccess

I am trying to rewrite the url using htaccess and I have tried answers given on another questions here however nothing seems to be working at all I still get the original url.
this is what I have:
http://localhost/inbox.php?pg=2
I want
http://localhost/inbox/2
I already had a rule that gets rid of the .php extension in .htaccess as below and just added the last line
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [QSA,L]
RewriteRule /(.*)$ /inbox.php?pg=$1//added this
Your problem is that the line before the last one is defined as the last one so your rule must be above that RewriteConditions. Better use this rule set:
RewriteRule ^/?inbox/(\d+)$ /inbox.php?pg=$1 [QSD,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [QSA,L]
I added your needed prefix which you missed and made it mandetory that after that numbers will follow (at least one).
Apache rewrite engine is mainly used to turn dynamic url’s such as http://localhost/inbox.php?pg=2 into static and user friendly url’s http://localhost/inbox/2
RewriteEngine on
RewriteRule ^inbox/([^/.]+)/?$ /inbox.php?pg=$1 [L]
Explanation: How this work
Call to action: RewriteRule
Pattern: ^inbox/([^/.]+)/?$
Rewrite: /inbox.php?pg=$1
Command Flag: [L]
Simply,
RewriteEngine on
RewriteRule ^inbox/([0-9]+)/?$ inbox.php?pg=$1
Source: 10 Simple examples to rewrite using htaccess

a little help in figuring out htaccess rules and why they aren't working correctly

I have these rules and I can't figure out why one of them won't work:
RewriteEngine On
RewriteBase /
Options +FollowSymlinks
Options -MultiViews
Options -Indexes
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^error/([^/.]+)/([^/.]+)$ /pages/error.php?$1=$2 [NC,R]
RewriteRule ^([^/.]+)$ index.php?user=$1 [NC,L]
RewriteRule ^([^/.]+)/.* index.php?$0 [PT]
RewriteRule ^error/.* error.php/?$0 [PT]
Rule 1 works, echo $_GET['user'] then I get results
Rule 2 works as well. If I do a print_r($_SERVER['REQUEST_URI']) then I get what ever is after .com/.
so That makes me think that its catching correctly, and if I do a as well.
So those rules seem to be working... but now if I enter
domain.com/error/access/notallowed then I get a Not Found error, so that means that my Rule 3 that wants to catch anything that starts with error isn't working.
Any ideas on what may be causing this behavior?
EDIT: updated rules based on answer
RewriteRule ^([^/.]+)$ index.php?user=$1 [NC,L]
RewriteCond %{REQUEST_URI} ^/error/?
RewriteRule ^([^/.]+)/?.* index.php?$1 [PT]
RewriteRule ^(error/?.*)$ /_msp/pages/error.php/?$1
The problem is that your second RewriteRule is catching domain.com/error/access/notallowed before apache is able to make it to the third RewriteRule. So, apache says, "I've got a rule that matches, I'm finished here," and throws away the rest of your RewriteRules. If only there were a way we could tell apache to ignore the second rule in specific cases ...
Say hello to RewriteCond
"Wait, are you saying what I think you're saying? I can tell apache to ignore that second rule?"
"Yes, that's exactly what I'm saying."
RewriteRule ^([^/.]+)$ index.php?user=$1 [NC,L]
RewriteCond %{REQUEST_URI} !^/error/?
RewriteRule ^([^/.]+)/?.* index.php?$1 [PT]
RewriteRule ^(error/.*)$ index.php?$1 [PT]
See that RewriteCond line just before your second rule? You can insert any number of conditions and they will apply to the next evaluated rule. So, in this specific RewriteCond we're telling apache that the following rule shouldn't be used if the REQUEST_URI starts with /error/.
The rewritten URL using the above rules for the address domain.com/error/access/notallowed will be:
index.php/?error/access/notallowed
You can adjust the regex according to your needs.
I believe if you switch the last two it should work:
RewriteRule ^error/.* error.php/?$0 [PT]
RewriteRule ^([^/.]+)/.* index.php?$0 [PT]
RewriteRule ^([^/.]+)/.* index.php?$0 [PT] rule is matching the url, I believe rewriting it and passing it through as the index.php, but I am not a htaccess expert.

Categories