Forum Moderators: phranque

Message Too Old, No Replies

Can not get my htaccess correct! 3 week+ of T/A and still cant get it.

I Really need htacess mod-Rewrite help!

         

mynyhart

10:59 pm on Oct 5, 2010 (gmt 0)

10+ Year Member



Ok, Hello Webmasterworld.com. Glad i was redirected to this site for help because i posted my problem over at Sitepoint and Got 0 help at all with my problem other then critique on how janky my code was. But no solutions.

I Have a Image cms script running on my website and i have spent countless hours trying to shorten the URLS and make them SEO Friendly. I have Tried So many Differnt Mod-Rewrite Setups and still can not get it right after reading and searching and asking for help with this lol.


Ill explain everything from scratch.
I have www.myspace-help.com < - Main URL

First Page is the Main Catagory.
The ?page=1 is the Pagination it can go like ?page=2 and so on for how many pages there is.
ex. - www.myspace-help.com/Background/?page=1

When u go to the first main catagory u have a choice of looking into Sub-Cats. They range from things like abstract, cute-wallpaper, my-birth-day, As folder names and then the /?page=1 pagination.
ex. - www.myspace-help.com/Background/guitar/?page=1

There is also About 13 Other Folders/Catagorys Useing this script.
www.myspace-help.com/Myspace-Icons/?page=1
www.myspace-help.com/Avatar/?page=1
www.myspace-help.com/Myspace-Online-Icons/?page=1

And inside those are also 100+ Sub-Catagories in each Main Catagory
www.myspace-help.com/Myspace-Icons/cute/?page=1
www.myspace-help.com/Avatar/cool/?page=1
www.myspace-help.com/Myspace-Online-Icons/pink/?page=1

After all of this I have a Veiw.php Script...... Someone on
www.myspace-help.com/Background/guitar/?page=1
Choses
www.myspace-help.com/Background/view.php?cat=guitar&layout=guys-2.jpg


This is my Current Setup and it is only working 50% of the pages.

# Turns www.myspace-help.com/Background/?page=1 to
# www.myspace-help.com/Background/1.htm
RewriteRule ^(.*)/(\d+)\.htm$ $1/?page=$2 [L,NC]

# www.myspace-help.com/Background/guitar/?page=1 to
# www.myspace-help.com/Background/guitar/1.htm
RewriteRule ^([a-zA-Z-_]+)/([a-zA-Z-_]+)/(\d+)\.htm$ $1/2$/?page=$3[L,NC]

# And www.myspace-help.com/Background/guitar/1.htm to
# www.myspace-help.com/Background-guitar-1.htm
RewriteRule ^([a-zA-Z-_]+)-([A-Za-z0-9-_]+)-(\d+)\.htm$ $1/$2/$3.htm [L,NC]

# And www.myspace-help.com/Background/1.htm to www.Myspace-help.com/Background_1.htm
RewriteRule ^([A-Za-z0-9-_]+)_(\d+)\.htm$ $1/$2.htm [L,NC]

# and finnally www.myspace-help.com/Background/view.php?cat=guitar&layout=guys-2.jpg
# to www.myspace-help.com/Background-guitar-guys-2.jpg.htm
RewriteRule ^([a-zA-Z-_]+)-([A-Za-z0-9-_]+)-([A-Za-z0-9-_]+).(gif|jpg|swf|flv|png).htm$ $1/view.php?cat=$2&layout=$3.$4 [L,NC]


Problems i have right now with my htacess is if i have Folders like
/Background/Emo/ - /Background/Emo-Images/ - /Background/Emo-Layouts/

With this line
"RewriteRule ^([a-zA-Z-_]+)-([A-Za-z0-9-_]+)-([A-Za-z0-9-_]+).(gif|jpg|swf|flv|png).htm$ $1/view.php?cat=$2&layout=$3.$4 [L,NC]"

Only Emo will work the rest will 404. This failure also applies to my Sub-Cats alone.
Sometimes i can view
www.myspace-help.com/background-emo-1.htm But not
www.myspace-help.com/background-emo-images-1.htm

and with view.php sometimes ill be inside say /emo-images/ but trying to pull the url using my mod rewrite for view.php will return a 404 instead of the image. But on other images with the same circumstances. Also things like images named 4.jpg or myspace-icon-pink-1.jpg would just 404 for no reason when other images in the same cat work.....
My Htacess is just plain bad!


Threw trial and error i did one thing on my side that should help out this htaccess Rewrite
I Renamed every single file and folder into lowercase, Use only - instead of _ Ever, And Renamed Everyfile after its sub-cat like /background/guitar/ would have images inside named "guitar-01.jpg" or "guitar-02.gif" All of my images are gif,jpg,png not just jpg or just gif.

But this is everything that is going on and i have spent way to long researching, reading, testing, even asking other forums and friends and im left with no currect htaccess seo url rewrite. Also i still dont fully understand how the [L] [NC] [R=301] [QSA] Mean or work or when they are needed. So if they look out of place, they are.


But Very long explination even shorter.
TLDR;

Ok
So This is what i have
www.myspace-help.com/Background/?page=1
www.myspace-help.com/Background/guitar/?page=1
www.myspace-help.com/Background/view.php?cat=guitar&layout=guys-2.jpg
How can i have
www.myspace-help.com/Background-1.htm
www.myspace-help.com/Background-guitar-1.htm

and for - www.myspace-help.com/Background/view.php?cat=guitar&layout=guys-2.jpg
www.myspace-help.com/Background-guys-2.jpg
www.myspace-help.com/Background-guys-2.html / htm / php
www.myspace-help.com/Background-guys-2/
www.myspace-help.com/Background-guitar-guys-2.jpg
www.myspace-help.com/Background-guitar-guys-2/
www.myspace-help.com/Background-guitar-guys-2.html / htm / php

Anything close to one of those top 6


I Will forever be thankful if someone could kindly help me finnaly squash this problem!

My Email is Calm.to.catastrophe@gmail.com (also MSN) Thank you WebmasterWorld.com!

jdMorgan

3:30 am on Oct 6, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The problem is not the code. The problem is that you need to "design" the URL-structure of your site so that the proper "disposition" of each requested URL can be determined easily, using simple rules and simple regex patterns.

So stop coding and start thinking of a good solid URL design that will result in simple rules, and that will survive growth or changes on your site.

Also, I must note that using resource-names like "xyz.jpg.html" is not a good practice. It will confuse some clients, and throw big red flags in some security software. Either the "thing" is an HTML page, or it's a jpeg image, and it should be so named.

Also, the comments on several of your rules are wrong, and that confusion may be making this harder for you to think about. The rules don't change URLs from this to that. The rules effectively just tell the server, "If the browser requests *this* filename, then pass the request to *that* script filepath" (plus a query string). The pseudo code here would be RewriteRule ^this$ /that.php?query=whatever [L]

I think you'd be better off by explicitly listing your categories, and detecting them with explicit patterns, or by tagging your URLs -- an example would be /c-<category-here>/s-<subcategory-here>/p-<page-number-here>
This is just an example, but as you can see, it now becomes very easy to determine what is a category and what is a subcategory, and what is a page number -- for both humans and for computers using regular expressions pattern-matching. You can't constantly be "fighting" URLs that threaten to match multiple ambiguous patterns. That's a potential maintenance nightmare, and may severely limit your options to 'grow' the site and its URL-space.

The bottom line is that each URL must match the pattern of one and only one rule, and further that the output from any rule must never match the pattern of any other rule, or you will get a rewriting loop (and unexpected and likely-undesirable results).

Jim

mynyhart

8:04 am on Oct 9, 2010 (gmt 0)

10+ Year Member



Thank you so much for your response jdMorgan. that is a very good idea and i appreciate it.
I have redone my Code once again but only so i can try and finish up the last part of my Mod-Rewrite setup. So its very dynamic i believe but i will clean it up so it picks up less pages.

# www.myspace-help.com/Backgrounds/?page=1 to
# www.myspace-help.com/Backgrounds-1.php
RewriteRule ^([^/]+)-(\d+)\.php$ $1/?page=$2 [L,NC]

# www.myspace-help.com/Backgrounds/Saint_Patrics_Day/?page=1 to
# www.myspace-help.com/Background-Saint_Patrics_Day-1.htm
RewriteRule ^([^/]+)-([^/]+)-(\d+)\.htm$ $1/$2/?page=$3 [L,NC]

# www.myspace-help.com/Background/view.php?cat=Saint_Patrics_Day&layout=19.gif to
# www.myspace-help.com/Background-Saint_Patrics_Day-19.gif
RewriteRule ^([^/]+)-([^/]+)-([^/]+)/?$ $1/view.php?cat=$2&layout=$3 [L,NC]


The Problem i am having is with my last line. How can i make
"www.myspace-help.com/Background-Saint_Patrics_Day-19.gif" become
www.myspace-help.com/Background-Saint_Patrics_Day-19.html

The problem i have is that i have many Image types. gif,jpg,and png
The second Problem i am having with that last line is that It creates Duplicate Content.
I have
www.myspace-help.com/Background-Saint_Patrics_Day-19.gif
and www.myspace-help.com/Background/view.php?cat=Saint_Patrics_Day&layout=19.gif

But i can not figure out how to get anyone who goes to www.myspace-help.com/Background/view.php?cat=Saint_Patrics_Day&layout=19.gif to be 301'd to the new page www.myspace-help.com/Background-Saint_Patrics_Day-19.gif.

The First two lines inside the CMS Script it has a 301 allready built in but the View.php Does not. And ive tried fooling with it and i cant do it threw the php so im trying to do it threw the htacess. I thought it would be simple....
If anyone goes to www.myspace-help.com/Background/view.php?(.*) 301 to the real page or even a index but i cant get that to work.

So TLDR

# www.myspace-help.com/Background/view.php?cat=Saint_Patrics_Day&layout=19.gif to
# www.myspace-help.com/Background-Saint_Patrics_Day-19.gif
RewriteRule ^([^/]+)-([^/]+)-([^/]+)/?$ $1/view.php?cat=$2&layout=$3 [L,NC]


How can i make
www.myspace-help.com/Background-Saint_Patrics_Day-19.gif into
www.myspace-help.com/Background-Saint_Patrics_Day-19.html
or even
www.myspace-help.com/Background/Saint_Patrics_Day-19/
Keep in mind i have gif,jpg,and png files

And how do i 301
www.myspace-help.com/Background/view.php?cat=Saint_Patrics_Day&layout=19.gif to
www.myspace-help.com/Background-Saint_Patrics_Day-19.gif

or even 301 it to www.Myspace-help.com I just dont want any of the view.php? Pages to be shown so i dont have duplicate content.

jdMorgan

2:44 pm on Oct 9, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You're still confusing URLs with filepaths, and using terminology that's too loose to make sense... Please couch your questions in terms of

"How do I redirect client requests for the URL http://example.com/images/foo/bar.html to the new/correct URL http://example.com/foo/bar.gif"

or

"How do I rewrite client requests for the URL http://example.com/images/foo/bar.html so that the server serves the contents of the filepath /foo2/bar.gif"

Note that these are two very different functions, and the only two major functions that mod_rewrite can do; It does not "make URLs" or "change the address bar," and any such requests are simply ambiguous and confusing.

I'm trying (and apparently not very well) to get you over this "hump" in the learning curve, because some of your questions above simply don't make sense. Using the two question templates above may help you as well to start thinking in terms of what mod_rewrite actually can and does do, and this will make things much simpler for you to understand.

The one question I can answer is this one;
And how do i 301
www.myspace-help.com/Background/view.php?cat=Saint_Patrics_Day&layout=19.gif to
www.myspace-help.com/Background-Saint_Patrics_Day-19.gif

RewriteCond %{THE_REQUEST} ^[A-Z]+\ /Background/view\.php\?cat=Saint_Patrics_Day&layout=19\.gif\ HTTP/
RewriteRule ^Background/view\.php$ http://www.myspace-help.com/Background-Saint_Patrics_Day-19.gif? [R=301,L]

This code is complex because it explicitly requires that the script path be requested directly by the HTTP client. If the script path is being internally requested as a result of a previously-invoked URL-to-filepath rewrite, then this rule will not be invoked. This is required to prevent an 'infinite' rewrite/redirect loop from happening when your other rule(s) map requests for static URLs to to dynamic filepaths.

Jim

jdMorgan

3:23 pm on Oct 9, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



More on this:
The Problem i am having is with my last line. How can i make
"www.myspace-help.com/Background-Saint_Patrics_Day-19.gif" become
www.myspace-help.com/Background-Saint_Patrics_Day-19.html

The rule does not mention ".gif" or ".html", and your question doesn't mention "view.php" or the trailing slash, and that is likely the major part of the problem. In order to write correct and optimized code, the requirements must be must be correct and complete.

Your other rules aren't commented correctly, and have incorrect/non-optimal patterns. For those, I'd suggest:

# Internally rewrite requested URL-paths like /Backgrounds-1.php to filepath /Backgrounds/?page=1
RewriteRule ^([^/-]+)-(\d+)\.php$ /$1/?page=$2 [L]
#
# Internally rewrite requested URL-paths like /Background-Saint_Patrics_Day-1.htm to filepath /Backgrounds/Saint_Patrics_Day/?page=1
RewriteRule ^([^/-]+)-([^/-]+)-(\d+)\.htm$ /$1/$2/?page=$3 [L]
#
# Internally rewrite requested URL-paths like/Background-Saint_Patrics_Day-19.gif to filepath /Background/view.php?cat=Saint_Patrics_Day&layout=19.gif
RewriteRule ^([^/-]+)-([^/-]+)-([^/]+)/?$ /$1/view.php?cat=$2&layout=$3 [L]

The comments now reflect what the rules actually do, whether they do what you really want or not.

Note that the last rule will redirect any request of the abc-def-ghi form, regardless of the "filetype."

The [NC] flags were removed because none of your patterns contained any explicitly-declared alphabetic characters, making [NC] an unnecessary waste of CPU resources.

The leading slashes on the substitution paths close down a well-known exploit.

Jim

mynyhart

3:59 pm on Oct 9, 2010 (gmt 0)

10+ Year Member



Thank you for your response. I am using the very basic commands for right now just so i can get it going, i will rewrite them with tighter syntex's so its not so dynamic with what it rewrites.

And thank you for the example on how to do a Rewritecond . I have been thinking the only way for me to get the url im looking for is by doing a rewrite cond {query string} but i have not figured i out because most examples online are for query strings in the base url such as
myspace.com/view.php?cat=test&layout=objects.gif
where as mine are all in there own folder.

mynyhart

4:01 pm on Oct 9, 2010 (gmt 0)

10+ Year Member



and i understand about the "Note that the last rule will redirect any request of the abc-def-ghi form, regardless of the "filetype." "

That is why i posted the question here on the forum because i could not figure out how to make a mod rewrite do what i wanted
which is

to take
/Background/view.php?cat=Saint_Patrics_Day&layout=19.gif
to
/Background-Saint_Patrics_Day-19/
or
/Background-Saint_Patrics_Day-19.html

g1smd

6:53 pm on Oct 9, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Unless you state which of those is a URL "used out on the web" and which of those is a filepath "used inside the server" there's no way to comment on possible code. See jdMorgan's previous post.

mynyhart

7:08 pm on Oct 9, 2010 (gmt 0)

10+ Year Member



Ok i have
/Background/view.php?cat=Saint_Patrics_Day&layout=19.gif

URL to image
/Background/Saint_Patrics_Day/19.gif

mynyhart

7:36 pm on Oct 9, 2010 (gmt 0)

10+ Year Member



Ok i did this

(i will go back threw later and fix all the syntex to make it more accurate to what needs to be pulled.)

RewriteBase /
RewriteRule ^([^/-]+)-(\d+)\.php$ /$1/?page=$2 [L]
RewriteRule ^([^/-]+)-([^/-]+)-(\d+)\.htm$ /$1/$2/?page=$3 [L]
RewriteRule ^([^/-]+)-([^/-]+)-([^/]+)/?$ /$1/view.php?cat=$2&layout=$3 [L]
RewriteCond %{THE_REQUEST} ^[A-Z]+\ ([^&]+)/view\.php\?cat=([^&]+)&layout=([^&]+)\ HTTP/
RewriteRule /view\.php$ %1-%2-%3? [R=301,L]

This correctly shortens all the urls i want
And the last 3 lines 301 directs
/Background/view.php?cat=Saint_Patrics_Day&layout=19.gif to
/Background-Saint_Patrics_Day-19.gif
and works great. But is there a way to make it so i can use a
/Background-Saint_Patrics_Day-19.html
even tho i have 3 types of images gif,jpg,and png

jdMorgan

1:50 pm on Oct 15, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, but it's likely going to be complicated and very inefficient.

The third rule that rewrites ".html" requests to view.php with "layout=xyz" will have to be reproduced three times, with each of these three rules checking for the existence of an actual image file of one type. This presumes that the images actually do exist somewhere as physical files.

In that case, take the ".html" URL, convert that to an image filepath, and then use a RewriteCond to see if that image file exists with each filetype in turn. Then if so, rewrite to view.php using this filetype as the extension to the "layout=" variable value.

The order in which you check the image filetypes will determine their priority if more than one image filetype exists for a given .html URL-path. If this is of no concern, then check the filetypes in order from most-frequently-requested to least for the sake of efficiency. Example for .gif and .jpg:

# Rewrite ".html" URL-path requests to view.php with ".gif" layout if .gif file physically exists
RewriteCond %{DOCUMENT_ROOT}/path-to-physical-images/$3.gif -f
RewriteRule ^([^/-]+)-([^/-]+)-([^/.]+)\.html/?$ /$1/view.php?cat=$2&layout=$3.gif [L]
#
# Rewrite "html" URL-path requests to view.php with ".jpg" layout if .jpg file physically exists
RewriteCond %{DOCUMENT_ROOT}/path-to-physical-images/$3.jpg -f
RewriteRule ^([^/-]+)-([^/-]+)-([^/.]+)\.html/?$ /$1/view.php?cat=$2&layout=$3.jpg [L]

This is just an example. You will need to work out the exact construct needed to correctly build the physical image filepath to be checked, and it can be quite tricky and difficult to debug. Also, this method cannot work if these images are not stored in separate physical files. In this case, you'll have to work out some way for the view.php script itself to "discover" the correct filetype (using a database lookup, perhaps).

The fourth rule --the redirect-- is simpler, since any of the three image filetypes will be redirected back to ".html" URLs. By the way, this fourth rule should really be the first, in order to comport with the general recommendation that all external redirects should be listed first, followed by all internal rewrites, with each of these two groups in order from most-specific patterns and conditions to least-specific.

Jim