rewrite url htaccess cakephp paginator - php

RewriteRule result/([^\.]+) /job/search/result?k=$1 [L]
i have this code working well with urls like this
www.example.com/job/search/result/keyword
will give me the result of "keyword"
but the problem is that cakephp paginator is generating a link
like this
result/page:2?k=keyword
how can i write a rule for that
to fix the problem, i tried this but without a luck
RewriteRule result\/page:([0-9]+)\?k=([^\.]+) /job/search/result/page:$1?k=$2 [L]
so my new rules are
RewriteRule result\/page:([0-9]+)\?k=([^\.]+) /job/search/result/page:$1?k=$2 [L]
RewriteRule result/([^\.]+) /job/search/result?k=$1 [L]

I've fixed it by adding a rewriting condition
RewriteCond %{REQUEST_URI} !^/job/search/result/page:
so that the rewrite rule will only respond to
/job/search/result/keyword

Related

.htaccess RewriteRule for category/page in custom CMS

I have a working RewriteRule that rewrites any page to the literal url:
RewriteRule ^(.+)$ index.php?page=$1 [QSA,L]
The problem is I want to add page=$1&category=$2 and convert it to...obviously... /category/page
I tried this, but it doesn't seem to work:
RewriteRule ^([^/]+)/([^/]+)$ index.php?page=$1&category=$2 [QSA,L]
I should note that I would still like to be able to access pages without categories - ie /login or /about that should go to index.php?page=about for instance
Your solution will be as below.
RewriteRule ^([0-9a-zA-Z_\-]+)/([0-9a-zA-Z_\-]+)$ index.php?page=$1&category=$2 [NC,L]
Input url : http://www.test.com/my-cat/my-page
and it will be treated as : http://www.test.com/index.php?page=my-cat&category=my-page
Try with below, we are instructing apache to don't look for directory or file.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)$ index.php?page=$1&category=$2 [QSA,L]
try this
#https://www.example.com/index.php?category=9&page=CBSE `#https://www.example.com/category/page/CBSE`
Method One:
#use this code for generate new url
RewriteRule ^index/([0-9a-zA-Z]+)/([0-9a-zA-Z]+)$ index.php?category=$1&page=$2 [NC,L]
Method Two :
RewriteRule ^index/([0-9a-zA-Z]+)/([0-9a-zA-Z]+)$ index.php?category=$1&page=$2
Note :Hit Your Url index.php?category=$1&page=$2 to convert $i & $2 Create Dynamic Url Your Id Bases
This will work for you.
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)$ /index.php?category=$1&page=$2 [L]
If you want to generate rewrite urls easily, there are a lot online rewrite url generators.
Thanks for all the contributions...
Because I needed to cater for both /page and /category/page...I ended up doing this:
RewriteRule ^([0-9a-zA-Z_\-][^/]+)$ index.php?page=$1 [N]
RewriteRule ^([0-9a-zA-Z_\-]+)/([0-9a-zA-Z_\-]+)$ index.php?category=$1&page=$2 [L]
But that was only half the solution as I needed to modify my PHP to check whether category is set or not and to send a 404 if it doesn't match. Otherwise /invalid_category/valid_pagewould still work.
The ideas above all pushed me in the right direction, thanks.

What am I doing wrong with this .htaccess?

I have the following .htaccess file:
RewriteEngine On
RewriteBase /
RewriteRule ^/(.*)/(.*)/(.*)$ index.php?pageLevel1=$1&pageLevel2=$2&pageLevel3=$3 [L,R=301]
When I access my website using http://www.domain.com/testpage/ it gives me the 404 Not Found error. What am I doing wrong?
PS: currently the index.php files just echo the pageLevel1, pageLevel2 and pageLevel3 values.
Remove the leading slash from Rewrite pattern
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)/(.*)/(.*)$ index.php?pageLevel1=$1&pageLevel2=$2&pageLevel3=$3 [L,R=301]
First of, I don't know why are you using the 301 redirection at all? If you want to accept all characters in the url you just need to have a catch-all rewrite rule like this:
RewriteRule ^(.*)$ index.php?param=$1 [L]
If you want to catch the parameters like you defined, the rewrite rules can be like this:
RewriteRule ^([A-Za-z-]+)/([A-Za-z-]+)/([A-Za-z-]+)$ index.php?pageLevel1=$1&pageLevel2=$2&pageLevel3=$3 [L]
UPDATE:
The completed rules in your case (three page levels), with URL slugs which accepts letters and numbers can be like:
RewriteRule ^([A-Za-z0-9-]+)$ index.php?pageLevel1=$1&pageLevel2=$2 [L]
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)$ index.php?pageLevel1=$1&pageLevel2=$2 [L]
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)$ index.php?pageLevel1=$1&pageLevel2=$2&pageLevel3=$3 [L]
This way you can access pages like:
www.domain.com/test
www.domain.com/test/another-test
www.domain.com/test/another-test/new-level

.htaccess SEO Friendly URLs not working with two parameters

I am trying to create SEO friendly URLs for my site and I have come to problem that I can´t solve. I can´t figure out how to setup the rewrite rule so it will show for example like this www.mysite.com/shows/late-night and www.mysite.com/news/title-of-article. Here is my htaccess file:
RewriteEngine on
RewriteRule ^(.*)/([a-z_-]+) index.php?switch=$1&shows=$2 [NC,L]
RewriteRule ^(.*)/([a-z_-]+) index.php?switch=$1&article=$2 [NC,L]
RewriteRule ^shows/$ index.php?switch=shows [NC,L]
RewriteRule ^articles/$ index.php?switch=article [NC,L]
The rewrite rule works if I comment out the first and the third line, but how I do so both will work.
1st and 2nd rule cannot coexist because they are matching same URI pattern. Only first will all the time. Tweak your rules like this:
RewriteEngine on
RewriteRule ^(shows)/([a-z_-]+)/?$ index.php?switch=$1&shows=$2 [NC,L,QSA]
RewriteRule ^(news)/([a-z_-]+)/?$ index.php?switch=$1&article=$2 [NC,L,QSA]
RewriteRule ^(shows|news|articles)/?$ index.php?switch=$1 [NC,L,QSA]

How to rewrite somedomain.com/free/single/result_id to somedomain.com/f/single/result_id

I'm having trouble getting my head around rewrite.
I'm trying to make it so that the URL somedomain.com/f/single/1 would grab the result from somedomain.com/free/single/1
I've tried the following.
RewriteRule ^/f$ /free$1 [L]
Here is a copy of my existing .htaccess with a suggestion made by Kamil...
RewriteEngine on
RewriteCond $1 !^(index\.php|assets|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
RewriteRule ^f/(.+)$ /free/$1 [L]
I wonder if the problem is that I am already removing index.php before the /free ?
Can anyone help me understand what I'm doing wrong?
You look for following rule:
RewriteRule ^f/(.+)$ /free/$1 [L]
The first part ^f/(.+)$ is regular expression - it matches all strings starting with /f/ and stores the rest to the $1... then it's rewritten to /free/ and the rest stored in $1.

htaccess rewrite not working for images

Appraciate any help anyone can give with this,
I have an old url structure of: products/category(category is dynamic and only used as an example) and i'm trying to change to category/products, to do this I used
RewriteRule ^(.*)/products index.php?category=$1
This is working however problem is my images are stored as images/products/image and the rule is writing all my image links,
I've tried:
RewriteRule ^images/products/(.*) images/products/$1
just to rewrite images but it doesn't seam to be working
my htaccess file looks like this:
RewriteRule ^images/products/(.*) images/products/$1
RewriteRule ^(.*)/products index.php?category=$1
Thanks in advance,
Dan
The easiest would be to add a rule to not rewrite any paths of existing directories and files (your images...).
You can do that by adding these rules before your rewrite rule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/products index.php?category=$1
Jeroen's answer works as well, but you can also specify a rule as an end point
RewriteRule ^images/products/(.*) images/products/$1 [L]
RewriteRule ^(.*)/products index.php?category=$1
The '[L]' should prevent the second rule from being parsed as the first rule should work as expected.

Categories