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]
Related
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]
I have made a custom PHP/Sql cms with a dynamic post template called blog-post.php
I am pulling in the content like so:
http://www.example.com/news/blog-post.php?slug=sample-slug-from-database
This allows me to pull in any number of posts using the same template, pulling info from the database.
I have spent 4 hours trying to achieve the following with htaccess ..
Force a trailing slash instead of .php extension
Force blog post urls to show as:
http://www.example.com/sample-slug-from-database/
instead of
http://www.example.com/news/blog-post.php?slug=sample-slug-from-database
Any help is greatly appreciated.
Although your preferred method of URL is to redirect everything, e.g. /sample-slug-from-database/, I think it would be better to instead go with a less strict implementation, e.g. /news/sample-slug-from-database/
Either way, here's the first implementation:
# URL
# INPUT http://www.example.com/random-slug/
# OUTPUT http://www.example.com/news/blog-post.php?slug=random-slug
RewriteEngine On
RewriteRule ^(.*)\/$ news/blog-post.php?slug=$1 [L,QSA]
And the second way:
# URL
# INPUT http://www.example.com/news/random-slug/
# OUTPUT http://www.example.com/news/blog-post.php?slug=random-slug
RewriteEngine On
RewriteRule ^news\/(.*?)(\/(.*?)*)$ /news/blog-post.php?slug=$1 [L]
For anyone else struggling - this is my final code:
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1/ [R,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/$
RewriteRule . %{REQUEST_URI}/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
RewriteEngine On
RewriteRule ^(.*)\/$ blog-post.php?slug=$1 [L,QSA]
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]
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" }
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /domain.com/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*?)/?$ /domain.com/$1.php [L]
am using this code for remove question mark but its not working, trying http://domain.com/details?id=71 to http://domain.com/details/id/71
Please help me where am wrong?
Thanks in advance
I'm not sure what you're trying to accomplish with the first rewrite, but for the second (the last three lines), if the incoming URI is not an actual directory or file, you want to rewrite something like details/id/71 to /details.php?id=71?
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ /$1.php?$2=$3 [L]
should get you close, assuming there are always 3 fields. In a RewriteRule you don't put the domain name again.
Your code doesn't do anything remotely close to what you want. Your code simply removes the php extension when a request is made explicitly with the extension and adds it back internally. In order to make http://domain.com/details?id=71 get redirected to http://domain.com/details/id/71, you need:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\s/+([^/]+?)(?:\.php|)\?([^=]+)=([^&\ ]+)
RewriteRule ^ /%1/%2/%3? [L,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ /$1?$2=$3 [L]
you want this before your rules that remove the php extension
Assuming your script is called details.php, the following single rule:
RewriteRule details/(.*)/(.*)/$ /details.php?$1=$2
Should rewrite www.domain.com/details/id/71 to www.domain.com/details.php?id=71
You don't need all that other stuff to remove the .php extension.