Pagination URL Structure In Htaccess - apache

I have a URL Structure as mysite.com/category.php?c=abc&page=4
I need to have a URL Structure as mysite.com/category/abc/page/4
My Htaccess File code for this rewrite looks like this.
RewriteEngine On
<IfModule mod_rewrite.c>
https redirect Rule Here
</IfModule>
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^([-\w]+)$ blog-post.php?slug=$1 [NC,L]
RewriteRule ^amp/([-\w]+)$ amp/amp.php?slug=$1 [NC,L]
RewriteRule ^mirror/([-\w]+)$ mirror/post.php?slug=$1 [NC,L]
RewriteRule ^category/([a-zA-Z-]+) category.php?c=$1 [NC,L]
RewriteRule ^category/([a-zA-Z-/]+)/page/([0-9]+) category.php?c=$1&page=$2 [NC,L]
RewriteRule ^mirror/category/([a-zA-Z-]+) mirror/category.php?c=$1 [NC,L]
RewriteRule ^feed/([a-zA-Z-]+)$ feed/feed.php?f=$1 [NC,L]
RewriteRule ^author/([a-zA-Z0-9-]+) author.php?name=$1 [NC,L]
RewriteRule ^tag/([a-zA-Z0-9-]+) tag.php?t=$1 [NC,L]
RewriteRule ^mirror/tag/([a-zA-Z0-9-]+) mirror/tag.php?t=$1 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [R=301,L]
RewriteRule ^sitemap\.xml/?$ sitemap.php
RewriteRule ^news-sitemap\.xml/?$ news-sitemap.php
RewriteRule ^image-sitemap\.xml/?$ image-sitemap.php
RewriteRule ^author-sitemap\.xml/?$ author-sitemap.php
ErrorDocument 404 /404.php
ErrorDocument 500 Error
Options -Indexes
The problem I am facing is when I try to get the page number using the $_get[] variable, I am not able to fetch it. Rest I am able to get the ABC through $_get[]

You have big set of rules. Let me make an attempt to get you going:
ErrorDocument 404 /404.php
Options -Indexes -MultiViews
RewriteEngine On
<IfModule mod_rewrite.c>
https redirect Rule Here
</IfModule>
## Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.+)/+$
RewriteRule ^ %1 [R=301,NE,L]
## ignore all URIs for actual files and directories
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^amp/([-\w]+)$ amp/amp.php?slug=$1 [NC,L,QSA]
RewriteRule ^mirror/([-\w]+)$ mirror/post.php?slug=$1 [NC,L,QSA]
RewriteRule ^category/([\w-]+)$ category.php?c=$1 [NC,L,QSA]
RewriteRule ^category/([\w-]+)/page/(\d+)$ category.php?c=$1&page=$2 [NC,L,QSA]
RewriteRule ^mirror/category/([\w-]+)$ mirror/category.php?c=$1 [NC,L,QSA]
RewriteRule ^feed/([\w-]+)$ feed/feed.php?f=$1 [NC,L,QSA]
RewriteRule ^author/([\w-]+)$ author.php?name=$1 [NC,L,QSA]
RewriteRule ^tag/([\w-]+)$ tag.php?t=$1 [NC,L,QSA]
RewriteRule ^mirror/tag/([\w-]+)$ mirror/tag.php?t=$1 [NC,L,QSA]
RewriteRule ^sitemap\.xml$ sitemap.php [NC,L]
RewriteRule ^((?:news|image|author)-sitemap)\.xml$ $1.php [NC,L]
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
RewriteRule ^([-\w]+)$ blog-post.php?slug=$1 [QSA,L]
Make sure to test it after clearing your browser cache completely or test it from command line curl.

With your shown samples, please try following. Please make sure to clear your browser cache before testing your URLs.
There are 2 important points you need to take care. 1st- You didn't use $ anchor which makes sure your 1st rule matching only URI like http://locahost:80/category/testtest so what's happening your rules are matching both the URIs. 2nd- Is you need to add conditions to make sure these rules are applied to only non existing stuff(in ideal scenario).
RewriteEngine On
Options -Indexes
<IfModule mod_rewrite.c>
##https redirect Rule Here
</IfModule>
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.*)$ $1.php [L]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^([-\w]+)$ blog-post.php?slug=$1 [NC,L]
RewriteRule ^amp/([-\w]+)$ amp/amp.php?slug=$1 [NC,L]
RewriteRule ^mirror/([-\w]+)$ mirror/post.php?slug=$1 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^category/([a-zA-Z-]+)/?$ category.php?c=$1 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^category/([a-zA-Z-/]+)/page/([0-9]+)/?$ category.php?c=$1&page=$2 [NC,L]
RewriteRule ^mirror/category/([a-zA-Z-]+) mirror/category.php?c=$1 [NC,L]
RewriteRule ^feed/([a-zA-Z-]+)$ feed/feed.php?f=$1 [NC,L]
RewriteRule ^author/([a-zA-Z0-9-]+)$ author.php?name=$1 [NC,L]
RewriteRule ^tag/([a-zA-Z0-9-]+)$ tag.php?t=$1 [NC,L]
RewriteRule ^mirror/tag/([a-zA-Z0-9-]+)$ mirror/tag.php?t=$1 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [R=301,L]
RewriteRule ^sitemap\.xml/?$ sitemap.php [NC,L]
RewriteRule ^news-sitemap\.xml/?$ news-sitemap.php [NC,L]
RewriteRule ^image-sitemap\.xml/?$ image-sitemap.php [NC,L]
RewriteRule ^author-sitemap\.xml/?$ author-sitemap.php [NC,L]
ErrorDocument 404 /404.php
ErrorDocument 500 Error

Related

error 404 redirect not working with my .htaccess

I tried usingĀ 
ErrorDocument 404 /abc/404.php
But when the wrong URL is entered it redirects to the main page, not to the 404 error page
I have a htaccess file with this code:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{THE_REQUEST} /search(?:\.php)?\?search=([^\s&]+) [NC]
RewriteRule ^ search/%1? [R=302,L,NE]
RewriteRule ^search/(.*)/page/(.*)/?$ search.php?search=$1&page=$2 [NC,L]
RewriteRule ^search/(.*)$ search.php?search=$1 [L,QSA,NC]
RewriteRule ^category/(.*)/page/(.*)/?$ category.php?id=$1&page=$2 [NC,L]
RewriteRule ^category/(.*)$ category.php?id=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ viewpost.php?id=$1 [QSA,L]
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
ErrorDocument 404 /abc/404.php
These two rules...
RewriteRule ^(.*)$ viewpost.php?id=$1 [QSA,L]
RewriteRule ^(.*)$ index.php/$1 [L]
Replace any URL that is accessed with the replacement... Unless the accessed URL is a real file/directory. As per the RewriteCond rules preceding them:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
If you don't want to divert every request that isn't a real file/directory then you need to change those rules...

htaccess fails to redirect when encounter whitespace

I am using htaccess for clean url. Which is working fine as long as the parameters doesn't carry whitespace. In which case it converts the space in %20 and I receive 404 error.
So basically the URL: http://localhost:8080/series/dynamics/admin/cleanURL/green%20apple
gives me 404 error. But URL: http://localhost:8080/series/dynamics/admin/cleanURL/greenapple works fine.
Also is there a way I can remove the directory details from the URL, I tried
RewriteRule ^series/dynamics/admin/cleanURL/(.*)$ /$1 [L,NC,R]
But doesn't work
htaccess
<IfModule mod_rewrite.c>
Options +MultiViews
Rewriteengine on
RewriteBase /series/dynamics/admin/cleanURL/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^series/dynamics/admin/cleanURL/(.*)$ /$1 [L,NC,R]
Rewriterule ^([a-zA-Z0-9]+)/?$ index.php?product=$1
RewriteRule ^(.*)(\s|%20)(.*)$ /$3-$4 [R=301,L,NE]
#rewrite group and subgroup e.g. http://.../value1/value2/ [L,NC,QSA]
Rewriterule ^([a-zA-Z0-9]+(.*)+)/([^/]+(.*)+)/?$ index.php?product=$1&subgroup=$2 [L,NC,QSA]
</IfModule>
You will need to add this rule in your site root .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) series/dynamics/admin/cleanURL/$1 [L]
Then have these rules in /series/dynamics/admin/cleanURL/.htaccess:
Options +MultiViews
Rewriteengine on
RewriteBase /series/dynamics/admin/cleanURL/
RewriteCond %{THE_REQUEST} /series/dynamics/admin/cleanURL/(\S*)\s [NC]
RewriteRule ^ /%1 [L,R=302,NE]
RewriteRule ^([^\s\x20]*)[\s\x20]+(.*)$ $1-$2 [L,R=302,NE]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
Rewriterule ^(\w+)/?$ index.php?product=$1 [L,QSA]
#rewrite group and subgroup e.g. http://.../value1/value2/ [L,NC,QSA]
Rewriterule ^([a-zA-Z0-9]+(.*))/([^/]+(.*))/?$ index.php?product=$1&subgroup=$2 [L,NC,QSA]

.htaccess rewriterule for username like facebook

I am utilising following code for my current application
RewriteBase /
#for profile display
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^user/([^/]+)/([^/]+)/([^/]+)/?$ /profile.php?username=$1&option=$2&source=$3 [QSA]
RewriteRule ^user/([^/]+)/([^/]+)/?$ /profile.php?username=$1&option=$2 [QSA]
RewriteRule ^user/([^/]+)/?$ /profile.php?username=$1 [QSA]
#for hiding.php extension
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA,L]
What this will do is, it will treat URL www.domain.com/user/johndoe/photos/photoid-123 as www.domain.com/profile.php?username=johndoe&option=photos&source=photoid-123
Now, what I want to do is to remove /user/ part from the url and keep it like www.domain.com/johndoe/photos/photoid-123
I have tried modifying rewrite rule like RewriteRule ^([^/]+)/.... OR RewriteRule ^/([^/]+)/.... OR RewriteRule ([^/]+)/...., but it is giving me 500 Error
Any suggestions?
Have it this way:
DirectoryIndex index.html index.php
RewriteEngine On
RewriteBase /
#ignore files and directories from rewrites
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ profile.php?username=$1&option=$2&source=$3 [QSA,L]
RewriteRule ^([^/]+)/([^/]+)/?$ profile.php?username=$1&option=$2 [QSA,L]
RewriteRule ^([^/]+)/?$ profile.php?username=$1 [QSA,L]
#for hiding.php extension
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !\.php$ %{REQUEST_FILENAME}.php [QSA,L]

htaccess issue in accessing the directory

My Blog link is http://www.example.com/blog when i am trying to access it it converts the url into http://www.example.com/blog/?ID=blog which is not correct. But when i put the backslash at the end of the blog it works fine but i don't want this back slash at the end
I try to change every thing inside htaccess file but failed.
Here is my htaccess file
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_FILENAME} !-d [OR]
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^ - [L]
RewriteRule ^products products.php
RewriteRule ^websitemap sitemap.php
RewriteRule ^aboutus aboutus.php
RewriteRule ^clients clients.php
RewriteRule ^shoppingcart inq.php
RewriteRule ^contactus contact.php
RewriteRule ^finished finished.php
RewriteRule ^product/([a-zA-Z0-9_-]+)$ product_large.php?ID=$1
RewriteRule ^([a-zA-Z0-9_-]+)$ category.php?ID=$1
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ products.php?ID=$2&MID=$1
ErrorDocument 404 http://www.example.co.uk/error404.php
RewriteCond %{HTTP_HOST} ^example.co.uk$
RewriteRule ^/?$ "http\:\/\/www\.example\.co\.uk\/" [R=301,L]
Insert this rule after RewriteRule ^ - [L] to force a trailing slash:
DirectorySlash On
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{THE_REQUEST} \s/+(.*?)[^/][?\s]
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301]
RewriteRule ^blog(/.*)?$ - [L,NC]
# your rest of the rules

RewriteRule all except other rewrite already declared

I have the following entries handled as fixed files using .htaccess (for example)
RewriteEngine On
RewriteRule ^redir$ redir_base64.php
RewriteRule ^confirm$ confirm.php
RewriteRule ^migrate$ migrate.php
RewriteRule ^import_conn$ import_conn.php
RewriteRule ^callback_ot$ callback_ot.php
RewriteRule ^callback_yh$ callback_yh.php
RewriteRule ^callback_gp$ callback_gp.php
RewriteRule ^callback_lk$ callback_lk.php
RewriteRule ^results/map$ index.php
RewriteRule ^results$ index.php
RewriteRule ^robots\.txt$ robots.php
RewriteRule ^sitemap\.xml$ sitemap.php
RewriteRule ^removeml$ removeml.php
RewriteRule ^rss$ rss_gen.php
RewriteRule ^cpt\.jpg$ c/captcha/captcha.php
RewriteRule ^cptfrm\.jpg$ c/captcha/captcha.php?width=120&height=60&characters=4
RewriteRule ^cptfrm160\.jpg$ c/captcha/captcha.php?width=100&height=60&characters=3
RewriteRule ^login/lk$ login_conn.php?op=lk
RewriteRule ^t$ c/thumb/phpThumb.php
RewriteRule ^spce$ spce.php
RewriteRule ^orplkd$ order_publisher.php
RewriteRule ^ord_ren$ order_reactivate.php
RewriteRule ^logout$ logout.php
This entry works fine! But I need to access /000/000 and I see that an conflict with lines indicates below.
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\d\w\-\.]+)/([\d]+)$ index.php [QSA,L]
RewriteRule ^([\d\w\-\.]+)$ index.php [QSA,L]
I try to access to my site using that lines but I cannot to recognize only the last lines because use ALL access on the site.
I hope so understand my help.
Thanks!
You need to repeat the 2 conditions. Rewrite conditions only apply to the immediately following condition.
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\d\w\-\.]+)/([\d]+)$ index.php [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\d\w\-\.]+)$ index.php [QSA,L]