Need help with mod_rewrite - php

My current code is
RewriteRule ^(.*)$ index.php?pg=$1 [L,QSA]
index.php?action=product shop.com/product
index.php?action=product/add shop.com/product/add
Now I want to add paging to some of the pages, something like
index.php?action=product&page=1&show=20 shop.com/product/1/20
index.php?action=product/add&page=1&show=20 shop.com/product/add/1/20
How do I do this?

RewriteEngine On
RewriteRule ^product$ index.php?action=product
RewriteRule ^product/add$ index.php?action=product/add
RewriteRule ^product/(\d+)/(\d+)$ index.php?action=product&page=$1&show=$2
RewriteRule ^product/add/(\d+)/(\d+)$ index.php?action=product/add&page=$1&show=$2
I suggest taking a look at this answer too. I think it would be better in this case.

Related

How specify more general my .htaccess routing in php?

I have the following rules in .htaccess:
RewriteRule js/jquery.min.js myproject/page/js/jquery.min.js
RewriteRule js/bootstrap.min.js myproject/page/js/bootstrap.min.js
RewriteRule styles/bootstrap.min.css myproject/page/styles/bootstrap.min.css
RewriteRule styles/bootstrap-theme.min.css myproject/page/styles/bootstrap-theme.min.css
RewriteRule styles/vinoservice.css myproject/page/styles/vinoservice.css
And also for images like that.
And my question is how can i specify a route like: every js/.js file serving the myproject/page/js/.js and of course for the styles also?
Thanks for the helps!
UPDATE
I tried this for them:
RewriteRule ^js\/.*(.js$) vinoservice/page/js/$1
RewriteRule ^styles\/.*(.css$) vinoservice/page/styles/$1
RewriteRule ^images\/.*(.png$) vinoservice/page/images/$1
RewriteRule ^images\/.*(.jpg$) vinoservice/page/images/$1
But not working correctly...but in regex generator says its good for them...interesting...what can be a problem? :S
You should only be concerned with the extensions if you wish to handle js/myscript.php differently, which I doubt you do.
As such, use the following:
RewriteRule js/([^/]+) myproject/page/$1 [L,R=301]
RewriteRule styles/([^/]+) myproject/page/$1 [L,R=301]

.htaccess filename to url

I've been searching and cannot find an answer that suits my needs. It is a rather simple question I assume.
The point is that I need to rewrite my files to a name of my liking. For example I have, 'www.mydomain.com/account_index.php' which I want to rewrite to www.mydomain.com/account
Thus far I had this, but that doesn't seem to work with multiple lines.
Rewriteengine on
rewriterule ^support account_ticket.php [L]
rewriterule ^account account_index.php [L]
So in short, I want my website to redirect /account to account_index.php and the other way around.
Thanks in advance!
I found the answer for those that are wondering.
I had to put a RewriteCond before every RewriteRule.
So if I wanted to go to www.mydomain.com/account. I'd have this:
RewriteCond %{REQUEST_URI} ^/account$ [NC]
RewriteRule ^account account_index.php [NC,L]
This means that /account is now linked to account_index.php.
Have a nice day!

How to change php urls on my website?

Is it possible to somehow change my website links from:
domain.com/category.php?tag=test
domain.com/section.php?tag=test
domain.com/news.php?tag=test
into this:
domain.com/category-test
domain.com/section-test
domain.com/news-test
Thanks and have a good day!
You can achieve this using .htaccess and mod_rewrite. You'll need to create a .htaccess file with the following contents:
RewriteEngine On
RewriteBase /
RewriteRule ^category-([^/]+)/?$ category.php?tag=$1
RewriteRule ^section-([^/]+)/?$ section.php?tag=$1
RewriteRule ^news-([^/]+)/?$ news.php?tag=$1
Now, accessing domain.com/category-test/ will take you to category.php?tag=test.
If you'd prefer to have slashes instead of dashes, you can use:
RewriteRule ^category/([^/]+)/?$ category.php?tag=$1
RewriteRule ^section/([^/]+)/?$ section.php?tag=$1
RewriteRule ^news/([^/]+)/?$ news.php?tag=$1
RewriteRule ^category-([a-zA-Z0-9]+)$ category.php?tag=$1 [NC,L]
RewriteRule ^section-([a-zA-Z0-9]+)$ section.php?tag=$1 [NC,L]
RewriteRule ^news-([a-zA-Z0-9]+)$ news.php?tag=$1 [NC,L]
In "category.php" file get the tag $_GET['tag'];

htaccess multiple parameters rewrite rule

I know this problem is over asked, but couldnt find anything fitting with my problem.
I'm currently creating a website, and my url are like :
www.foo.com/
or www.foo.com/index.php.
They can take 1, 2 ,or three different parameters like
www.foo.com/index.php?page=Home&lang=en&article=1
What i'd like is an url like
www.foo.com/Home/
or www.foo.com/en/Home
or www.foo.com/Article/1
or www.foo.com/en/Article/1
The page parameter is required, other two are not..
I cant have anything working for me... Any help would be greately appreciated
Thanks a lot !
Better to have separate clean rules. Put this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteRule ^index\.php$ - [L]
RewriteRule ^([^/]+)/?$ /index.php?page=$1 [L,QSA]
RewriteRule ^([a-z]{2})/([^/]+)/?$ /index.php?page=$2&lang=$1 [L,QSA]
RewriteRule ^([a-z]{2})/([^/]+)/([0-9]+)/?$ /index.php?page=$2&lang=$1&article=$3 [L,QSA]
RewriteRule ^([^/]+)/([0-9]+)/?$ /index.php?page=$1&article=$2 [L,QSA]
Try something like this
RewriteRule ^([a-z0-9_-]+)/([a-z0-9_-]+)/([a-z0-9_-]+)\.html$ index.php?param1=$1&param2=$2&param3=$3

modRewrite issues, and modRewrite rules

Haay!
Have a question about ModRewrite for apace webserver.
recently i have fixed one of my urls:
Before: http://pagename.com/index.php?sideID=home
RewriteEngine on
RewriteRule ^([a-zA-Z0-9]+)$ index.php?sideID=$1
After : http://pagename.com/home
Now I have a more advanced problem when I want to pass more variables trough URL
to create my blogg.
currently: http://pagename.com/index.php?sideID=blogg&id=12&title=a-great-blog-post
I would like this to be more clean and structured, I want somthing like:
http://pagename.com/blogg/12/Gratulerer-FEEL-GOOD-med-ny-hjemmeside
I have tested something like this:
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ index.php?sideID=$1&id=$2&title=$3
without any success, any anwser leading to my success will be highly aprecciated :)
Here is my current .htaccess file:
RewriteEngine on
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?sideID=$1
RewriteRule ^bloggg/([^/]+)/([^/]+)/([^/]+)/?$ index.php?sideID=$1&id=$2&title=$3 [L,QSA]
Use this additional rule:
RewriteRule ^blogg/([^/]+)/([^/]+)/([^/]+)/?$ index.php?sideID=$1&id=$2&title=$3 [L,QSA]

Categories