URL Rewrite not working for multiple pages in same folder - .htaccess

Trying to improve another one of my parameter based urls. The first rewrite works, but the second fails to find relevant php based data:
<IfModule mod_rewrite.c>
RewriteEngine on
Rewritecond %{REQUEST_FILENAME} !/js
Rewritecond %{REQUEST_FILENAME} !/custom/contact
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)\.html?$ /photography/viewphoto.php?id=$1 [QSA,NC,L]
RewriteRule ^([^/]+)\.html?$ /photography/photoscollection.php?ref=$1 [QSA,NC,L]
</IfModule>
Anybody help?
UPDATE:
I currently have 5 pages containing photos, photoscollection01.html -> photoscollection05.html based on categories. The contents of the Categories are read fron an XML file. I decided to just have the single PHP file with a URL parameter representing the Category, i.e. ref.
From within one of these pages, a user can click a photo to view it, based on its id.
Therefore:
page containing photos.
/photography/photoscollection.php?ref=photos01 -> /photos01.html etc
view specific photo.
/photography/viewphoto.php?id=c01-a01-p01 -> /c01-a01-p01.html

Related

Mod_Rewrite for URL with multiple variables | .htaccess

I develop an e-shop site with products so for this proposal i made a product page for each of products with Mod_Rewrite for URL with the code above in my .htaccess file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*/([a-zA-Z0-9_-]+)|([a-zA-Z0-9_-]+))$ ./product.php?name=$1 [L]
So when user clicks on the link www.example.com/Nike-Air goes to Nike-Air shoes page and etc.
What i would like to do is to add one parameter more in the url, let's say size
RewriteRule ^(.*/([a-zA-Z0-9_-]+)|([a-zA-Z0-9_-]+))$ ./product.php?name=$1&size=$2 [L]
but this parameter to not append in the url, still to displays www.example.com/Nike-Air
and also to be able to get the size data from $_GET method.
Is it possible?
Any ideas guys?

rewrite url and get variable

How can I get the second part of a url to be a variable, rewrite it, and provide a fancy URL.
So example www.mywebsite.com/AFL would rewrite to www.mywebsite.com/index.php?league=AFL but would still display as www.mywebsite.com/AFL.
I am using the same pages for multiple leagues. When a user signs up they establish a league name in the database and then on each page I look for the league variable to display the info.
I also have many different pages. So a user could go to www.mywebsite/AFL/schedule_teamdisplay.php?Team=ABC&Year=2014. This would rewrite to www.mywebsite.com/schedule_teamdisplay.php?league=ALF&Team=ABC&Year=2014.
The league will always be the second entry in the URL.
Thanks
You can put this code in your htaccess (which has to be in your root folder)
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)$ /index.php?league=$1 [L]
RewriteCond %{DOCUMENT_ROOT}/$2 -f
RewriteRule ^([^/]+)/([^/]+)$ /$2?league=$1 [L,QSA]

.htaccess redirect all images in image folder

Ok, so I have a small predicament. I have a large image gallery that has over 30,000 images inside of it. I've decided to change to a newer piece of software, and this new gallery is completely different in the way image URLs are handled. So a simple redirect isn't going to cut it anymore (there is no way to determine a connection between A and B, per se, because the old gallery used ID's whereas the new one uses titles for SEO).
However, I would like to still accomodate to the old URL's by redirecting visitors to the corresponding image files. The script that they were contained within maybe gone, but the database and ID's of each image are still there - I was thinking to just rename every image file to the match the ID in the database, and use a redirect rule somehow to link to them.
The old URL structure looked something like this:
domain.com/image/3343/
All of the images are contained within the /images/ folder. If I rename every image filename to match the ID's (as above), how can I use htaccess to redirect the URLs to these files?
Keep in mind.. I need a solution that doesn't involve 30,000 rows in a htaccess file :)
Try:
RedirectMatch 301 ^/image/([0-9]+)/$ /image$1.jpg
Or using mod_rewrite:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^image/([0-9]+)/$ /image/$1.jpg [L,R=301]
For inside the image folder:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9]+)/$ /image/$1.jpg [L,R=301]

htaccess seo friendly urls for all subfolders

i have script in index.php in ALL folders
need to make pretty url/seo friendly urls like this: site.com/folder1/something.html
now i have ugly urls like this: site.com/folder1/index.php?category=something
this part index.php?category=something is always the same in all subfolders, i just want to change it to more seo friendly like something.html
once again: find index.php?category=1$ and replace it with 1$.html
do not touch anything else, just this part of url
so when i visit:
site.com/another-subfolder/and-one-more-folder-here/index.php?category=something
need to see this in address bar:
site.com/another-subfolder/and-one-more-folder-here/something.html
i hope you get it?
i tried this with folder1 in htaccess in root
RewriteRule ^folder1/(.*).html$ folder1/index.php?category=$1 [L,R=301]
ok this works, but how can i make this to work for all subfolders accross the site like this:
site.com/folder145/index.php?category=something
site.com/subfolder/index.php?category=something
site.com/another-subfolder/and-one-more-folder-here/index.php?category=something
there will be 1000s of subfolders with different names so manually making RewriteRule for each subfolder won't work
any help is appreciated
First, you need to change all the links that you generate from the index.php?category= form to the category.html form.
Then in the htaccess file in your document root, you add these rules to internally rewrite the category.html form back to the index.php?category= form:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?/)?([^/]+)\.html$ /$1index.php?category=$2 [L]
Then, to point links you don't have any control over (or ones generated using a FORM) externally redirect any direct access to links in the index.php?category= form:
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ (/.*?)?/index\.php\?category=([^&\ ]+)
RewriteRule ^(.+?/)?index\.php$ /$1%3.html? [L,R=301]
This should match any and every subdirectory.

WordPress like .htacces nice urls [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
.htacces to create friendly URLs. Help needed
I want to build a script like WordPress for the "nice urls", i copied the .htaccess rows and made some modifications, but on the side of the PHP i don't know how to build it.
I don't understand how it works.
With the help of Daniel A White i found the class in WordPress files.
WP_Rewrite: http://xref.wordpress.org/trunk/WordPress/Rewrite/WP_Rewrite.html
Start with setting rewrite engine and follow sym links
RewriteEngine On
# Drop the .php -- or any other extension you use.
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
#Forces the site to switch to www.site.com - change the site/TDL below
RewriteCond %{HTTP_HOST} ^insuranceiwant\.com$
RewriteRule (.*) http://www.insuranceiwant.com/$1 [R=301,L]
Now name your pages something useful like site.com/contact-us
If you are pulling dynamic pages from a database, also add this:
Options +FollowSymLinks
Options +Indexes
RewriteRule ^([A-Za-z_]+)_([A-Z]+)/([A-Za-z0-9_-]+)$ /Resources/city.php?state=$1&region=$2&city=$3
That example shows that the dynamic page has a state name, state abbreviation, and a city pulling from the database and showing the page like so:
insuranceiwant.com/California_CA/Los_Angeles
For more information on regular expresion for your page names check out this useful site:
http://www.regular-expressions.info/

Resources