Revising my ReWrite Code - .htaccess

Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*)\.html$ fetch.php?id=$1
I have this in my htaccess file and it works fine, but I want to know if there is a way to rewrite this so that instead of just pulling the id # i can pull the category and title up instead or will i need to do something like this
RewriteRule (.*)/(.*)/(.*)\.html$ fetch.php?id=$1&category=$2&title=$3
Also if this is what I need to do, how will I go about changing my title to have no spaces and only have dashes inbetween them? I heard that urls don't like spaces. I have read somewhere else they used a php code where the spaces were turned into "-" or "_" to be read into the page but reverted back to be read in sql. Let me know if you need to know more about my situation. Thanks.
UPDATE!
RewriteRule (.*)/(.*)/(.*)\.html$ fetch.php?id=$1&category=$2&title=$3
This is the htaccess rule
<a href="<?php echo $row_getDisplay['id']; ?>/<?php echo $row_getDisplay['category']; ?>/<?php echo urlencode($row_getDisplay['title']); ?>">
that is my link .. this method I get it to pull up with id/category/title with no spaces but it has no css or images displaying. i'm pulling by ID because that's how my fetch.php is pulling it i don't know how to set it so it pulls by category and title.

this method I get it to pull up with id/category/title with no spaces but it has no css or images displaying. i'm pulling by ID because that's how my fetch.php is pulling it i don't know how to set it so it pulls by category and title.
This is probably because all your links are relative, and the base URI has changed from /something.html (i.e. /) to /something/foo/bar.html (i.e. /something/foo/) so all the relative links are now broken. Try adding this to the header of your pages:
<base href="/">

I would advise against doing this as this is not very extensible, you might want to try the following:
Add the following in your htaccess:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* fetch.php/%{REQUEST_URI} [QSA,L]
This will put your entire url into the path_info environment variable, which you can than use in your backend code.
The RewriteCond parts make sure the url only gets rewritten if the file requested doen't actually exist.

Related

How to redirect a txt file to an html page with parameter and then call the content of that file

I'm trying to write a little RewriteRule/RewriteCond but can't find the right resource/tutorial to understand how to achieve my goal. I don't have much knownledge about regex and rewriterules.
That goal is to be able apply a markdown library like Strapdown to a markdown text file when opening it. My idea was :
the user opens the markdown file at http://somedomain.com/myFile.md
RewriteEngine get that you want to do that and redirect you to something like http://somedomain.com/markdown.html?myFile.md
that markdown html page will fetch the content of the markdown file (the filename is in the url parameters) and apply the css library to whatever is loaded and injected in a div.
I think I understood how to redirect the md file to the html document with parameter :
RewriteEngine On
RewriteRule ^(.*).md /markdown.html?md=$1 [QSA,L]
But then the markdown.html page that fetch the .md file is actually fetching the content of markdown.html itself (because of that rule).
So my idea then was to add some character at the end of the filename (like adding a '?' at the end of the file name : "myFile.md?") to be able to catch it in the rewrite rules.
RewriteEngine On
RewriteCond %{REQUEST_URI} !^(.*)\?$
RewriteRule ^(.*).md /markdown.html?md=$1 [QSA,L]
But it's not the way to go ... OR my last rewrite synthax is wrong and I don't understand why.
Any ideas ?
Thanks for the help !
I think the issue was that I was trying to use an indetectable pattern (ie : finding '?' at the end of my url).
I changed it to try finding any number as parameter and it works
RewriteEngine On
RewriteCond %{QUERY_STRING} !^([0-9])$
RewriteRule ^(.*).md /markdown.html?md=$1 [QSA,L]
The markdown.html file is now receiving the requested .md file as intended.
By seeing OP's answer I came with some additional improvements in it, please have your htaccess rule file as follows.
RewriteEngine On
RewriteCond %{QUERY_STRING} !^([0-9])$
RewriteRule ^(.*)\.md /markdown.html?md=$1 [QSA,L,NC]
Improvements done in OP's answer:
First escape .md on left side of RewriteRule to make it as a literal character else it may match strings like amd OR bmd etc.
Second use NC flag in your RewriteRule to make it case in-sensitive so that either .md OR .MD both get match.
Also additional suggestion in case your query string could have more than 1 digits in it, change your [0-9] to [0-9]+ in RewriteCond section.

.htaccess remove duplicate part of a URL

some bug in my online catalog SEF URL generation created a situation where some category slug is added to the URL twice. to fix this i would like to use .htaccess to remove the this part of the URL and have the whole URL shift up one level including URL's which also include a product under them.
the duplicate occurrence is of the sub-category painting-tools under the category tools
so the wrong URL looks like this:
/store/items/catalog/tools/painting-tools/painting-tools
or
/store/items/catalog/tools/painting-tools/painting-tools/{some product}
instead of being the correct URL like this:
/store/items/catalog/tools/painting-tools
or
/store/items/catalog/tools/painting-tools/{some product}
and the URLs
/store/items/catalog/tools/painting-tools/
or
/store/items/catalog/tools/painting-tools/{some product}
should actually be like this
/store/items/catalog/tools
or
/store/items/catalog/tools/{some product}
tried using this rule, but its not working
RewriteRule ^/painting-tools/painting-tools/(.*) /painting-tools/$1 [QSA]
i think the ^ part is not correct since its not the beginning of the URL.
How can i fix it ?
thanks
You can use this back-reference based redirect rule as very first rule in your .htaccess:
RewriteRule ^(.+?/([^/]+))/\2(/.*)?$ $1/$3 [NE,L,R=302]
RewriteRule ^(.+?/tools)/painting-tools(?:/.*)?$ $1/ [L,NC,R=302]
It is capturing repeat value after initial part and grouping it in this sub-pattern ([^/]+). Later in the regex it is using back-reference \2 to make sure same captured value is repeated.
Make sure you have proper RewriteBase defined to either / or /store/ wherever your htaccess is located.
Update: As per discussion below:
RewriteRule ^(.+?/tools)/painting-tools2(/.*)?$ $1$2 [L,NC,R=302]
This will remove /painting-tools2 from URLs.

mod_rewrite .htaccess with %20 translate to -

I have been reading about .htaccess files for a couple of hours now and I think I'm starting to get the idea but I still need some help. I found various answers around SO but still unsure how to do this.
As far as I understand you write a rule for each page extension you want to 'prettify', so if you have something.php , anotherpage.php, thispage.php etc and they are expecting(will receive??) arguments, each needs its own rule. Is this correct?
The site I want to change has urls like this,
maindomain.com/sue.php?r=word1%20word2
and at least one page with two arguments
maindomain.com/kevin.php?r=place%20name&c=person%20name
So what I would like to make is
maindomain.com/sue/word1-word2/
maindomain.com/kevin/place-name/person-name/
Keeping this .php page and making it look like the directory. Most of the tutorials I have read deal with how to remove the .php page to which the argument is passed. But I want to keep it.
the problem I am forseeing is that all of the .php?r=parts of the url are the same ie sue.php?r=, kevin.php?r= and the .htaccess decides which URL to change based on the filename and then omits it. If I want to keep the file name will I have to change the ?r=
so that it is individual? I hope this make sense. So far I have this, but I'm sure it won't work.
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^([a-zA-Z0-9]+)/$1.php?r=$1
RewriteRule ^([a-zA-Z0-9]+)/$1.php?r=$1&c=$1
And I think I have to add ([^-]*) this in some part or some way so that it detects the %20 part of the URL, but then how do I convert it to -. Also, how are my $_GET functions going to work??
I hope my question makes sense
You're missing a space somewhere in those rules, but I think you've got the right idea in making 2 separate rules. The harder problem is converting all the - to spaces. Let's start with the conversion to GET variables:
# check that the "sue.php" actually exists:
RewriteCond %{REQUEST_URI} ^/([a-zA-Z0-9]+)/([^/]+)/?$
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^([a-zA-Z0-9]+)/([^/]+)/?$ /$1.php?r=$2 [L,QSA]
RewriteCond %{REQUEST_URI} ^/([a-zA-Z0-9]+)/([^/]+)/([^/]+)/?$
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^([a-zA-Z0-9]+)/([^/]+)/([^/]+)/?$ /$1.php?r=$2&c=$3 [L,QSA]
Those will take a URI that looks like /sue/blah/ and:
Extract the sue part
Check that /document_root/sue.php actually exists
rewrite /sue/blah/ to /sue.php?r=blah
Same thing applies to 2 word URI's
Something like /kevin/foo/bar/:
Extract the kevin part
Check that /document_root/kevin.php actually exists
3 rewrite /kevin/foo/bar/ to /kevin.php?r=foo&c=bar
Now, to get rid of the "-" and change them to spaces:
RewriteCond %{QUERY_STRING} ^(.*)(c|r)=([^&]+)-(.*)$
RewriteRule ^(.*)$ /$1?%1%2=%3\ %4 [L]
This looks a little messy but the condition matches the query string, looks for a c= or r= in the query string, matches against a - in the value of a c= or r=, then rewrites the query string to replace the - with a (note that the space gets encoded as a %20). This will remove all the - instances in the values of the GET parameters c and r and replace them with a space.

Parse Entire URL in ColdFusion

I am trying to figure out how to parse an entire URL in ColdFusion.
By entire URL I mean something link this - http://subdomain.domainname.com/crm/leads/view/
I can figure out the first part by using the CGI.HTTP_HOST which gives me - subdomain.domainname.com. I cannot figure out how to read the - crm/leads/view/ part of the URL.
Is there a variable I can use to read that? I found some UDFs that will parse a full string like that for me but I need to be able to pass it the full URL.
I am also using URL Rewriting so that complicates it some. The URL could be http://subdomain.domainname.com/crm/leads/view/ but the actual page that is serving is http://subdomain.domainname.com/public/index.cfm.
I know this is the way many Frameworks work where all URLs are routed to a file and then it some how parses the URL and directs it to a certain controller and action.
Here is a copy of my .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.cfm [NC,L]
Any feedback on this would be great.
Thanks!
Ok, I'm going to make a few assumptions based on the SES URLs above, what you're trying to do, and how to work it into CGI.PATH_INFO below.
Root directory, .htaccess file:
RewriteEngine On
RewriteCond %{THE_REQUEST} /crm/([^?\ ]+)
RewriteRule ^.*$ /public/index.cfm/%1 [NC,L]
/public directory. .htaccess file:
RewriteEngine Off
This will redirect into CGI.PATH_INFO, for parsing. Read on for how to parse that.
When you are using Search-Engine Safe (SES) URLs, where your keys/values are all delimited by forward slashes, ColdFusion will consider these in the CGI.PATH_INFO server variable, usually reserved for directories.
So, knowing that, use a simple extraction mechanism to parse it:
<cfset SESQueryString = CGI.PATH_INFO />
<cfset num_pairs = ListLen(SESQueryString,'/') />
<cfset keyVals = StructNew() />
<cfloop from="1" to="#num_pairs#" step="2" index="i">
<cfset keyVals[ListGetAt(SESQueryString,i,'/')] = ListGetAt(SESQueryString,i+1,'/') />
</cfloop>
<cfdump var=#keyVals#>
Keep in mind that this answer assumes you have an even number of key/val matches in CGI.PATH_INFO. Also remember that List* functions in CF will (in many cases) will throw away an empty list value, so you may think you have an even number, you actually do not.
Scan the list functions on Adobe LiveDocs or cfquickdocs.com to see if the list function you are working with has a parameter you can pass it that will cause it to force empty list values in your list to not be tossed away. One such function is ListToArray().

How to insert an "index.php" into every url using .htaccess?

Example: My Site gets called like that:
www.mysite.com/controller/method/parameter1/parameter2
Now, .htaccess needs to rewrite this URL into:
www.mysite.com/index.php/controller/method/parameter1/parameter2
But the problem is: In case of an img, css or js directory, no redirection should happen.
How can I achieve this? What must I put to .htaccess? I just added this line but nothing happens:
RewriteCond $1 !^(css|js|images)
I haven't tested it, but this should work:
RewriteRule !^((css|js|images)/.*)$ index.php%{REQUEST_URI} [L, NE]
%{REQUEST_URI} will be the original /controller/method... stuff, including the ?query part hopefully. NE prevents double escaping of stuff, and L means no further rules are applied.

Resources