.htaccess rewrite rule with pagination - apache

I'm trying to write a mod-rewrite rule to handle pagination links on my site.
I'd like my url structure to be this: http://www.mysite.com/classifieds/state/example?page=2
I've tried the following: ^classifieds/city/(.*)?page=(.*) classifieds.php?state=$1&page=$2 [L]
Any guidance would be much appreciated. Thanks

You will need QSA flag to carry over existing query string.
Try this rule:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteRule ^classifieds/state/([^/]+)/?$ classifieds.php?state=$1 [L,QSA]
RewriteRule ^classifieds/city/([^/]+)/?$ classifieds.php?city=$1 [L,QSA]

Related

Enable human readable URL's in .htaccess

Preface: Yes this question seems like duplicated, and I found related questions, but answers from there didnt help to me. :(
Hello, I want to add human readable URL's support for my PHP project. For now my URL quesry string looks like:
index.php?url=main/index
I would like to make it looks like:
index.php/main/index
I read following articles:
Stackoverflow
Cheatsheet
Stackoverflow
Stackoverflow
But when I do this:
var_dump($_GET['url']); // get empty array
, get empty array like no url parameter added.
My current .htaccess:
DirectoryIndex index.php
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)$ index.php?url=$1 [NC]
Can somebody help me please? Thanks!
URL: http://domain.com/index.php/controller/action
Rewritten URL: http://domain.com/index.php?url=controller/action
.htaccess
DirectoryIndex index.php
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^index.php/(.*)$ /index.php?url=$1 [L,QSA]
Explanation:
The .* in the pattern ^index.php/(.*)$ matches everything after index.php/ on the incoming URL. The parentheses helps to capture the part as variable $1, which is then added at the end of the substitution URL /index.php?url= + $1.
[L, QSA]:
L ignore other rewrite rules, if this fits.
QSA means query string append.
You have
index.php?url=main/index
Yuo want to rewrite (and then user redirect) to this
index.php/main/index
What about trying this?
DirectoryIndex index.php
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^url=([a-z]+)/([a-z]+)$
RewriteRule ^.*$ http://mydomain.site/index.php/%1/%2 [R=302,L]
R=301 or 302 depend on your need
On this example i assumed that on the original url there are only a-z chars.
Just to clarify (due the fact generally the process is inverse) on this way the users will be redirect, this rules does not convert links on your page.
In line
RewriteRule ^(.*)$ index.php?url=$1 [NC] the (.*) matches the part of the url up to '?' where the query begins.
To use the values passed in the query you need the QSA flag. Means Query String Append.
If Your URL : www.abcd.com/index.php/abcd/...
.htaccess
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
Try the below code, Only considered for index.php you can replace it as per your requirement. Let me know if you need further assistance.
DirectoryIndex index.php
Options +FollowSymLinks
RewriteEngine ON
RewriteBase /
RewriteRule ^(index\.php)/([0-9a-zA-Z\/_-]{1,99}) index.php?url=$2
What worked for me is:-
<?php var_dump($_GET); ?> this is the only thing I did on url http://localhost/baba.php/abcd . and original .htaccess file
DirectoryIndex baba.php
RewriteEngine ON
RewriteBase /
RewriteRule ^(baba\.php)*/([0-9a-zA-Z\/_-]{1,99}) baba.php?url=$2
For Magento 2.4 Magento will always drop any GET parameters from the URL in a htaccess rewrite. The only way I found to make it work was to create a rewrite module. The setup is here: https://magento.stackexchange.com/a/158811/109113

Match Question Mark in rewrite rule using .htaccess

I have tried it in a different way that is also not working for me.
Match Question Mark in rewrite rule
please help me on this
Redirect to different url
http://localhost/crb/index.html?q=xxxxxxxx
To
http://localhost/crb/demo/result?q=xxxxxxxx
I tried this
Options +FollowSymlinks
RewriteEngine on
rewriterule ^crb/index.html?q=xxxxxxxx(.*)$ http://localhost/crb/demo/result?q=xxxxxxxx$1 [r=301,nc]
To rewrite based on query string use :
RewriteEngine on
RewriteBase /
RewriteCond %{QUERY_STRING} ^q=xxxxxxxx(.*)$
RewriteRule ^/?crb/index.html http://localhost/crb/demo/result?q=xxxxxxxx%1 [R=301,NC]
You could try the Query String Append (QSA) flag:
RewriteRule ^crb/index.html$ crb/demo/result [NC,L,QSA]
This should append the existing query string (q=xxxxxxxx) to the new url.
https://wiki.apache.org/httpd/RewriteFlags/QSA
http://httpd.apache.org/docs/2.2/rewrite/flags.html#flag_qsa
If this code doesn't work, modify it a little and try again.

Re-writing URL's with Query Parameters such as Product ID's

There is so much content on the web for URL re-writes based on a number of scenario's that it makes it difficult to locate a solution to a specific query.
I'm just trying to clarify what the 'best' way to re-write URL's with query parameters to 'search engine friendly' URL's would be when using mod_rewrite on an apache server.
For example:
http://www.mydomain.com/product.php?pid=130
Would something like the following in .htaccess rewrite the above URL to a search engine friendly version?
RewriteEngine on
Options +FollowSymlinks
RewriteBase /
RewriteCond %{QUERY_STRING} ^pid=130$
RewriteRule ^$ /productid-130.html [L]
Any advice on this would be greatly appreciated.
Thanks in advance.
try adding the following to your .htaccess file
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
#if it is a productid html url, then capture the productid
RewriteCond %{REQUEST_URI} ^/productid-([0-9]+)\.html$ [NC]
#and rewrite to product.php with id
RewriteRule . product.php?pid=%1 [L]

htaccess mod_rewrite Shorten to a SEO friendly URL

I have this address
http://www.nfrases.com/tag.php?id_tag=10&id_frase=508
that I would like to make it a lot shorter and more SEO friendly to something like (I don't know ... open to suggestions) http://www.nfrases.com/tag/10/508 or http://www.tag10name.nfrases.com/508
I need help with how to code this, because a simple thing as trying to make the address always http://www.nfrases.com case the user goes to http://nfrases.com or www.nfrases.com I tried and failed :(
Used this code by the way:
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.nfrases.com$
RewriteRule ^(.*)$ http://www.nfrases.com/$1 [R=301]
http://www.nfrases.com/tag.php?id_tag=10&id_frase=508
becomes
http://www.nfrases.com/10/508
with this in your .htaccess
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteRule ^([0-9]*)/(.*)/?$ /tag.php?id_tag=$1&id_frase=$2 [L,QSA]
Untested. Let me know if it works for you.

.htaccess RewriteRule domain.com/page/page to domain.com?p=page/page

I'm trying to set up my .htaccess file. How would I go about getting this result:
Request: domain.com/page/page
Rewrite to: domain.com/index.php?p=page/page
I've tried:
RewriteRule ^(.*)$ index.php?p=$1 [L,QSA]
with no luck.
Okay I worked it out. I needed two rules:
RewriteRule ^([a-zA-Z0-9_\\s-]+)(/)?$ index.php?p=$1
RewriteRule ^([a-zA-Z0-9_\\s-]+)/([a-zA-Z0-9_\\s-]+)(/)?$ index.php?p=$1/$2
My php script didn't like it because with just
RewriteRule ^([a-zA-Z0-9_\\s-]+)(/)?$ index.php?p=$1
domain.com/no1/ would translate into domain.com/index.php?p=no1/ <--- didn't understand the end slash
Only problem with this solution is it only goes two virtual dirs deep but as that's all I need it's fine.
This is my first time using .htaccess so I'm sorry for my lack of understanding.
Try this code in your .htaccess file:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteRule ^(?!index\.php)(.*)$ /index.php?p=$1 [L,NC,QSA]
Negative lookahead will avoid infinite looping here.