RewriteRule help - php

I have successfully setup htaccess to do this:
domain.com/ad.php?ad_id=bmw_m3_2498224
INTO:
domain.com/ads/bmw_m3_2498224
However, I have a link on the page which makes the page submit to itself...
The link saves the ad inside a cookie:
domain.com/ad.php?ad_id=bmw_m3_2498224&save=1 // Note the 'save' variable
I need to make this work on the rewritten rule also, so this link:
domain.com/ads/bmw_m3_2498224/save
will save the cookie...
I have this so far which DOES NOT work for the save part:
RewriteRule ^annons/([a-zA-Z0-9_]+)$ ad.php?ad_id=$1 [NC,L]
How can I include another rule to accomplish what I want?
Thanks

A simple way would be to add this line above what you currently have:
RewriteRule ^annons/([a-zA-Z0-9_]+)/save$ ad.php?ad_id=$1&save=1 [NC,L]
RewriteRule ^annons/([a-zA-Z0-9_]+)$ ad.php?ad_id=$1 [NC,L]
This should get triggered before the general rule is and work just the same.

Related

Having trouble with .htaccess and clean URLs, need redirect on entering the "dirty" URL

I know there's a million similar questions on stuff like this, but clearly there's much I don't understand because I haven't been able to derive answers or a solution to my (as I understand it) fairly simple question.
Basically, I'm trying to get an old site back up, but want a more professional look to it this time round, which includes cleaning up the URLs. A typical page is as follows (hosted locally at the moment, but will be assigned a domain in next few days):
192.168.0.200/album-reviews.php?albid=22
Using the following code, I have been able to achieve the above example page loading via manually typing 192.168.0.200/album-reviews/22 into the browser:
RewriteRule ^album-reviews/([0-9a-zA-Z]+) album-reviews.php?albid=$1 [NC,L]
However, what I want as well is for when the link is clicked on my site, it directs the user to /album-reviews/22 instead of album-reviews.php?albid=22. The only way to get the clean URL at the moment is to manually type it into the bar, links from my site do not get the clean URL, the code I have been playing around with (and have been unable to get working) based on sources I've found is this:
RewriteCond %{THE_REQUEST} /album-reviews/?(?:\.php)?\?albid=([0-9a-zA-Z]+)
RewriteRule ^ /album-reviews/%1? [L,R]
So if anyone could shed some light on how I get all this working as desired, I'd be grateful, I hope my question has been articulated appropriately.
On a side note, If i wanted to include the post title in the URL too like this:
192.168.0.200/album-reviews.php?albid=22&ptitle=my first post
how would alter any code to make it like this:
192.168.0.200/album-reviews/22/my first post
Thank you.
You can use:
Options -MultiViews
RewriteEngine on
RewriteCond %{THE_REQUEST} /album-reviews/?(?:\.php)?\?albid=([^\s&]+)&ptitle=([^\s&]+)
RewriteRule ^ album-reviews/%1/%2? [L,R=301]
RewriteCond %{THE_REQUEST} /album-reviews/?(?:\.php)?\?albid=([^\s&]+)
RewriteRule ^ album-reviews/%1? [L,R=301]
RewriteRule ^album-reviews/([^/]+)/?$ album-reviews.php?albid=$1 [NC,L]
RewriteRule ^album-reviews/([^/]+)/([^/]+)/?$ album-reviews.php?albid=$1&ptitle=$2 [NC,L]
If you want to show the contents of http://yoursite.com/album-reviews.php?albid=22 at the url http://yoursite.com/album-reviews/22, you need these codes:
Your htaccess needs this lines:
RewriteEngine On
Options -Indexes
RewriteRule ^album-reviews/(.*)$ album-reviews.php?pretty=$1 [L,QSA]
And your PHP these:
$pretty = $_GET['pretty'];
$parameters = explode('/',$pretty);
$albid = $parameters[0];
Your user won't be redirected, your website will show directly the page at the pretty url.
Now, what happened? That you instructed htaccess to send everything after album-reviews as a GET parameter called "pretty". Then in your PHP you cut it for every / that appeared in it, and that way you formed the array $parameters. So you can even get more parameters, for every /, they are all in the array $parameters:
$second_parameter = $parameters[1];
$third_parameter = $parameters[2];
$fourth_parameter = $parameters[3];

About RewriteRule and .htaccess

I have a problem:
I want to rewrite this:
example.com/pages/?page=start
or
example.com/pages/?page=news
to:
example.com/start
or
example.com/news
So I want the GET from the subpage to be displayed as a main page, does anyone know how?
If you want that the user enter something like example.com/start or example.com/news but the server should process it like example.com/pages/?page=start or example.com/pages/?page=news this is done as follows:
RewriteEngine On
RewriteRule ^/?([^/]+) /pages/?page=$1 [QSA,L]
Be careful here because example.com/news/first-news will also be rewritten to example.com/pages/?page=news and /first-news will be lost!

Excluding a path from a htaccess rule

Our .htaccesss file has the following rules which is affecting access to our admin pages.
The rules are setup to allow something like the following:
http://www.example.com/en-gb/section/product/
#Most pages are caught by this so we can use the translated token stored in cms rather than having to fix here
RewriteRule ^([a-zA-Z]{2})-([a-zA-Z]{2})/([a-zA-Z0-9\-_]+)(/?)$ index.php?html/pages/$3 [L,NC]
#Product pages
RewriteRule ^([a-zA-Z]{2})-([a-zA-Z]{2})/([a-zA-Z0-9\-_]+)/([a-zA-Z0-9\-_]+)(/?)$ index.php?html/pages/$4 [L,NC]
#Product subscription pages
RewriteRule ^([a-zA-Z]{2})-([a-zA-Z]{2})/([a-zA-Z0-9\-_]+)/([a-zA-Z0-9\-_]+)/([a-zA-Z0-9\-_]+)(/?)$ index.php?html/pages/$5 [L,NC]
Unfortunately this is affecting the admin pages, e.g. http://www.example.com/en-gb/admin which is firing a 404.
Basically, is there any way to ignore the above rule if the path contains /admin?
I've tried a RewriteCond before the RewriteRule but it doesn't seem to be working.
Cheers!
Thanks #JustinIurman but I couldn't get your answer to work unfortunately.
It was a much simpler solution than I thought in the end.
I just changed the rule to the following:
+RewriteRule ^([a-zA-Z]{2})-([a-zA-Z]{2})/(?!admin)([a-zA-Z0-9\-_]+)(/?)$
Thanks for your help.

htaccess Rewrite Rule Exception

I have the following code:
RewriteEngine On
RewriteRule ^user/([0-9]+)/([0-9]+)/?$ template.php?user_id=$1&slide=$2 [NC,L,QSA]
So when I go to www.mydomain.com/user/7/1 it is working, and pulling from www.mydomain.com/template.php?user_id=7&slide=1
Now my question is, how do I get the slide variable to be optional. I want it to work if someone will type in /user/7/.
In my template.php file I have:
if(!$current_slide) {
header("Location: user/".$user_id."/1");
}
So if I manually go to template.php?user_id=1 it will automatically redirect to /user/7/1/ which is good, it works. But I need to modify my htaccess file so if someone types in /user/7/ it will redirect to /7/1/
Hope that makes sense.
RewriteRule ^user/([0-9]+)/?([0-9]*)/?$ template.php?user_id=$1&slide=$2 [NC,L,QSA]

.htaccess with pagination in php?

I am currently coding a pagination script into many parts of my site, this has been a well needed and requested feature and I have finally been able to come round and start coding it, it is all going well, until I find that my rewritten urls don't like working with the pagination urls.
So, an example page on my site would be news.php. This file structure can be something like news.php?id=5. I have rewritten the url like so:
/news/5/
## Rewrite URL's for News & Dev ##
RewriteRule ^news/([0-9]+)/$ /news.php?id=$1 [L]
RewriteRule ^news/([0-9]+)$ /news.php?id=$1 [L]
RewriteRule ^news$ /news.php [L]
RewriteRule ^news/$ /news.php [L]
The pagination script I am using prepends two new variables in the url, the new url turns out to be this:
news.php?page=1&ipp=55id=5
I would really appreciate it if anyone could assist me in making the urls look better, as it defeats the object of having it in the first place if after they use the pagination, it changes the url back to a clunky and ugly url.
I don't want it to be required to have parts of the url, that is something I really don't want..
e.g I don't want the url to be required to be /news/1/55/5, instead id like it to be optional.
Thank you for your time, it is appreciated!
Additional Information
The links in my news script currently display like so:
news.php?page=1&ipp=55id=5
I don't like to see ugly urls like that, and want to make the url look better using mod_rewrite, It would be better if the urls would display like so:
/news/PAGE/IPP/ID/ -> return news.php?page=1&ipp=55id=5
Also, to make it as user friendly as possible, I don't want any of the fields to be required as such, so for example I would like to have the following link accessible at all times without it requiring the other fields.
/news/ID/
Then, when the user clicks a pagination link, it would use the following link structure:
/news/PAGE/IPP/ID/ -> return news.php?page=1&ipp=55id=5
This is all from user feedback of my site, and is something that people have been asking for. Problem is, I don't understand even simple .htaccess
Thanks
RewriteBase /
# add slash to end of url if not present (and do a redirect)
RewriteCond $0 !/$
RewriteRule ^news([^\.]*)$ $0/ [L,R=302]
# rewrite url with format /news/[<id>/[<page>/[<ipp>/]]]
RewriteRule ^news/(?:([0-9]+)/)?(?:([0-9]+)/)?(?:([0-9]+)/)?$ /news.php?id=$1&page=$2&ipp=$3 [L]
Not sure what ipp is supposed to be, but my guess is it shows the number of item per page. I would personally not like to have that in my url.
You can have :
news/id/page/ipp with
RewriteRule ^news(/?)$ news.php [L]
RewriteRule ^news/([0-9]+)-(.*)_([0-9]+)(/?)$ news.php?page=$1&ipp=$2&id=$3 [L]
news/1222-subjet-for-example_34
return :
news.php?page=1222&ipp=subject-for-example&id=34
use (/?) instead of create many rules ;)
Hope it's works for you.

Categories