Rewriting a url with more than one page - php

I'm trying to rewrite my urls on a page that has a pagination option and I'm unsure of what string to use because the one i'm currently using isn't working.
My document structure is below
blog-listing.php
htaccess
/blog/post.php
/css/
/js/
/images/
What's already in my htaccess file
RewriteEngine on
RewriteRule ^blog/category/([a-zA-Z0-9-/]+)$ /blog/archive.php?cat=$1 [L]
RewriteRule ^blog/category/([a-zA-Z0-9-/]+)/page/([0-9]+)/{0,1}$ /blog/archive.php?cat=$1&page=$2 [L]
RewriteRule ^blog/([a-zA-Z0-9-/]+)$ /blog/post.php?s=$1 [L]
RewriteRule ^blog/([a-zA-Z0-9-/]+)$ /blog/post.php?s=$1&page=$2 [L]
RewriteRule ^(css)($|/) - [L]
RewriteRule ^(perch)($|/) - [L]
RewriteRule ^(images)($|/) - [L]
RewriteRule ^(js)($|/) - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
The line i've tried to use
RewriteRule ^blog-listing/([a-zA-Z0-9-/]+)$ /blog-listing.php?s=$1&page=$2 [L]
The url I'm receiving
http://mysite(dot)com/blog-listing.php?page=2
The url I'd like
http://mysite(dot)com/blog-listing/page-2
Any ideas?

Use a simple file to manage your url
RewriteEngine On
# Redirect all requests not pointing at an actual file to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]
https://github.com/ogallardop/blackjack-api/blob/master/src/.htaccess
The framework you're using to pagination will do the rest.

Related

why my htaccess rule not works

I created a htaccess rule but it doesnot works.
here is my url
http://localhost/mat/site/brandlisting/21/page/2
here is my htaccess rule for it
RewriteRule brandlisting/(.*)(.*)/page/(.*) site/brandlisting?id=$1&page=$2 [L]
RewriteRule brandlisting/(.*)(.*)/page site/brandlisting?id=$1&page=$2 [L]
RewriteRule brandlisting/(.*)/ site/brandlisting?id=$1 [L]
RewriteRule brandlisting/(.*) site/brandlisting?id=$1 [L]
RewriteRule site/brandlisting/(.*)/?$ site/brandlisting?id=$1 [L]
is there anything wrong?
what mistate is in my htaccess rule.
You are using wildcard greedy pattern regex matching everything in single hit, use below rules I am assuming you are using .htaccess in site folder.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^brandlisting/([\d]+)/page/([\d]+)$ brandlisting?id=$1&page=$2 [L]
RewriteRule ^brandlisting/([\d]+)/?$ brandlisting?id=$1 [L]
RewriteRule ^site/brandlisting/([\d]+)/?$ brandlisting?id=$1 [L]

mod_rewrite and apache clean hyperlinks

i've a php website and using mod_rewrite for clean urls.
my files:
villa.php about.php contact.php index.php 404.php
my villa.php is dynamic and works with mysql db. url example:
domain.com/villa?i=2&s=villaname
i want to use it like domain.com/villa/id/villaname
my .htaccess file looks like this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [L,NC]
RewriteRule ^yazlik/(.*)/(.*)$ yazlik.php?i=$1&s=$2[L,NC]
but its not working. what is the right configuration for .htaccess file? any help would be great. thanks for your time.
You can use this rule in your htaccess file
RewriteRule ^villa/([0-9]*)/(.*)/?$ /villa?i=$1&s=$2 [QSA,NC,L]
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [L,NC]
RewriteRule ^yazlik/(.*)/(.*)$ yazlik.php?i=$1&s=$2[L,NC]
RewriteRule ^villa/([0-9]*)/(.*)/?$ /villa?i=$1&s=$2 [QSA,NC,L]

Redirect every link in php application

I have made a php web application which uses links like :
Link
... onClick="window.location.href='/asset/image.ext'" ...
<? header("Location: /login"); ?>
etc.
As you might have noticed, almost EVERY link starts with /.
Many links are rewritten in .htaccess like this :
Options All -Indexes
RewriteEngine on
RewriteRule ^login/error_418$ assets/common/login.php?&error [L]
RewriteRule ^login$ assets/common/login.php [L]
RewriteRule ^settings/change-profile-picture$ index.php?page=settings&command=profilePicture [L]
RewriteRule ^settings/change-password/error-requirements$ index.php?page=settings&command=password&error=requirements [L]
RewriteRule ^settings/change-password/error-missmatch$ index.php?page=settings&command=password&error=missmatch [L]
RewriteRule ^settings/change-password/error-418$ index.php?page=settings&command=password&error=418 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)/grades$ index.php?page=grades&course=$1 [L]
Now I need to move it on a web server where it will NOT be in the root direcotry but in a folder...
How do I now redirect all the root links to that folder ?
First place your existing rule in /folder/.htaccess like this:
Options All -Indexes
RewriteEngine on
RewriteBase /folder/
RewriteRule ^login/error_418$ assets/common/login.php?&error [L]
RewriteRule ^login$ assets/common/login.php [L]
RewriteRule ^settings/change-profile-picture$ index.php?page=settings&command=profilePicture [L]
RewriteRule ^settings/change-password/error-requirements$ index.php?page=settings&command=password&error=requirements [L]
RewriteRule ^settings/change-password/error-missmatch$ index.php?page=settings&command=password&error=missmatch [L]
RewriteRule ^settings/change-password/error-418$ index.php?page=settings&command=password&error=418 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)/grades$ index.php?page=grades&course=$1 [L]
Then create a rule in root .htaccess as this:
RewriteEngine On
RewriteBase /
RewriteRule ^((?!folder/).*)$ folder/$1 [L,NC]

htaccess rewriting rules not working

I am trying to rewrite the following url
http://localhost/foldername/index.php?url=writeboard/index&step=1&id=1
to
http://localhost/foldername/writeboard/index/step/1/id/1
Code is
RewriteRule ^(.+?)(?:/(step)/([0-9]+))?/?$ index.php?url=$1&$2=$3 [NC,L,QSA]
I tried with
RewriteRule ^(.+?)(?:/(step)/([0-9]+))(?:/(id)/([0-9]+))?/?$ index.php?url=$1&$2=$3&$4=$5 [NC,L,QSA]
it works but when the url becomes http://localhost/foldername/writeboard/index then I am getting 404.
This rule should work:
RewriteRule ^(.+?)/(step)/([0-9]+)/(id)/([0-9]+)/?$ index.php?url=$1&$2=$3&$4=$5 [NC,L,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+?)/?$ index.php?url=$1 [NC,L,QSA]
You should also add RewriteBase /foldername/ to your .htaccess file, since you're not in the top level.
More information about RewriteBase can be found here.

Combine htaccess rewrites

I am using php CodeIgniter and have the following rewrite rule in my .htaccess file:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Since I want the users to use the site through SSL, I would like to add the following rewrite rule:
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.link.com/$1 [R,L]
However, when I do that, every page in a subdirectory will have /index.php/actual_page - the index.php before that is not desired. I thought that might come from having RewriteRule ^(.*)$ index.php/$1 [L] in there, but without the RewriteRule ^(.*)$ https://www.link.com/$1 [R,L] it works fine.
So, how can I rewrite the url to https without breaking the first rewrite?
Make sure the https redirect happens before the rewrite to index.php. You want it to look something like this:
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.link.com/$1 [R,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

Categories