Expanding friendly url, mod rewrite - .htaccess

I am using modx, which is a cms system. It has a friendly url-option and uses a .htaccess like this.
# The Friendly URLs part
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
If makes the url look like: mysite.com/about and mysite.com/about/contact
I'd like to expand this .htaccess, so I can write myste.com/about/2 without the page 2 actually existing. I'd like to have the ability to access this in a get-variable. Can this be done? I have no knowledge at this area whatsoever.

You could do this in your .htaccess, but a better way might be to create a plugin that fires on the OnPageNotFound event.
myste.com/about/2 does not exist so it will trigger your plugin which will run before sending the user to the 404 page. In this case, you can can split the /2 from the end of the url and use it to pull in your data or send the user to a different page instead of going to 404.
Does that make sense? It's a bit difficult to contribute more without knowing exactly what you want to do :)

Related

htaccess URL rewrite pattern format

I've had a good look through all the other htaccess url rewrite questions, but all of them deal with the reverse of my problem.
The site I am working on takes content from child pages in WordPress and presents them as anchored sections on the parent page. The problem for the site now is that if Google (or the built in search, though that can probably be done in the templates) links to one of the child pages it will direct users to the single page rather than the correct section of the parent page.
I was hoping to come up with a URL rewrite pattern which would reformat the URL to what I need, but it doesn't seem to work... and I know that's because I have done it wrong!
The URL which needs to be rewritten is something like:
website.com/parent-page/child-page/
I need this to be rewritten to:
website.com/parent-page/?subpage=child-page
My initial stab at it looks like this, but I know I've misunderstood something about how to format the pattern.
RewriteRule ^/$1?subpage=$2 ^/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/$
Can anyone help format this correctly or point out where I'm going wrong?
You can use:
RewriteEngine on
# If the request is not for a valid file/directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)/?$ /$1/?subpage=$2 [QSA,L]
No initial / in htaccess RewriteRule first uri
And optional final /
RewriteRule ^([^/]+)/([^/]+)/$ /$1/?subpage=$2 [L]
The htaccess rewrites didn't seem to work, so I've created a PHP redirect instead by taking the page slug, page parent permalink and mashing the two together.
Not the most elegant solution, I expect, but it works.

Complex URL rewriting with .htaccess

Here is what i am trying to achieve
www.example.com/category
www.example.com/category/subcategory
www.example.com/category/subcategory/product
www.example.com/static-page (like /about-us, /contact, /our-services)
(category, subcategory, products, static-pages etc are dynamic text and there is a permalink foreach thing in the database)
if you see all above requests, you will notice that they are just like extending the directory sturcutre one step each time when the link is clicked, e.g, first was category, and then when I clicked on the subcatogery, I was sent to category/subcategory/ and the finally to product page
Anybody can help me how to acheive all this, i have tried lot to achieve this but in vain yet.
Currently i am using this .htaccess
RewriteRule c/(.*)/(.*)/ cat-details.php?permalink=$1&subcat=$2
RewriteRule c/(.*)/(.*) cat-details.php?permalink=$1&subcat=$2
RewriteRule news/(.*)/(.*)/ news-detail.php?news-id=$1&permalink=$2
RewriteRule news/(.*)/(.*) news-detail.php?news-id=$1&permalink=$2
RewriteRule d/(.*)/(.*)/(.*)/(.*)/ download-detail.php?download-id=$1category=$2&subcategory=$3&permalink=$4
RewriteRule d/(.*)/(.*)/(.*)/(.*) download-detail.php?download-id=$1category=$2&subcategory=$3&permalink=$4
As you can see i have to add c/, news/, d/ etc for each link, i am trying to get rid of this and want to make links pretty whitout proceeding c/, news/, d/ etc
About the only way you're going to be able to do this in Apache proper, is to provide info on which URLs map to which scripts. Apache lets you do this via the RewriteMap directive, but that won't work in .htaccess files. Without it, and without the news/ or c/, Apache doesn't have enough info to route the URLs properly.
What you could do, is simply rewrite every URL that doesn't refer to an existing file, to a script that knows which scripts to run in response. A "router" or "front controller" script, they call it. Something like this:
RewriteCond %{REQUEST_URI} !-f
RewriteCond %{REQUEST_URI} !-d
RewriteRule (.*) router.php?path=$1 [QSA]
And in the script, you examine $_GET['path'], decide which script should handle the request, and load/include/require it.
I have found a way myself, just posting it if it could help anyone else in trouble just like me :p
RewriteRule ^([^/]*)\/$ /romuniverse/cat.php?permalink=$1 [L]
RewriteRule ^([^/]*)/([^/]*)\/$ /romuniverse/sub-cat.php?permalink=$1&subcat=$2 [L]
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)\.rar$ /romuniverse/download-detail.php?download-id=$1&category=$2&subcategory=$3&permalink=$4 [L]

.htaccess directory redirect for version releasing

I had an idea which is killing me, as I am not very good with htaccess.
I have the url http://(www./non-www.)example.com/dir/page/file.
But I want to redirect this to the directory root/release/.../dir/page/file
So
root/.htaccess
root/release/1.0/...
root/release/1.5/...
root/release/2.0/...
root/admin/1.0/...
root/admin/1.5/...
root/admin/2.0/...
etc
And if they browse to example.com/admin you go to example.com/admin/2.0. But I think the tricky bit is that I dont want example.com/release/version or example.com/admin/version to be seen.
Or I could just have the two versions hosted release/stable and release/beta.
I hope that makes sense to someone,
Thanks
Sounds like the easiest thing would be to make a page like example.com/admin that meta-redirects to example.com/admin/2.0, then you just do a mod-rewrite to go back to the generic url. As you increase the versions, just change the meta refresh, because your .htaccess mod-rewrite will have reg-ex that matches to your version numbering system.
example:
First for the example.com/admin file, implement a meta-refresh in the header:
<meta http-equiv="refresh" content="0;URL='example.com/admin/2.0'">
Now do a re-write from the 2.0 (or from any version) back to where you want to go:
#first, we need to define the request, in this case, admin/any number
RewriteCond %{THE_REQUEST} /admin/([0-9]*)
# now we need to make it look like it's just the plain admin page
RewriteRule ^$ /admin [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^admin /admin/([0-9]*) [L]
you'll need to fix my reg-ex matches to make it 2 "point" 0

.htaccess file for pretty urls

(This seems like it should be one of the mostly commonly and easiest addressed questions on the web, since most websites have "pretty" or "clean" urls. But in all my searches, it's proven to be one of the most complex.)
In the simplest form, I would like be able to enter example.com/about into the url bar and have the server return the file example.com/about.php. As it is, I have to enter or link to example.com/about.php, which is not SEO or user friendly. This isn't about complex strings--the file could just as easily be example.com/about.html.
I have some code I'm attempting to use with an .htaccess file, but it seems to do nothing:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^([a-z]+)/$ /$1.php
I know that the .htaccess file is working, because the 404 redirect I have set up (which appears in the .htacces doc below the code I've included here) is functioning properly, especially when I'm trying access example.com/about and I get my 404 page.
Thanks for your help!
Your last rule is designed to match http://example.com/about/ . I think what you want is
RewriteRule ^([a-z]+)$ $1.php

Can I create a search engine friendly URL from this custom ColdFusion CMS URL?

I have inherited a custom ColdFusion CMS app. The URL's that it creates are horrendous. Not at all suitable for SEO or readability for that matter. An example of a URL in this CMS is:
http://www.mysite.com/Index2.cfm?a=000003,000010,000019,001335
Basically, each level of hierarchy is stored in the database based upon that long string of comma separated values. So in the case of the example I used, that particular page is 4 levels deep in the CMS hierarchy.
Basically what I would like to see is a format similar to this
http://www.mysite.com/level-1/level-2/level-3/level-4
Is this possible? Any help would be greatly appreciated. For what it's worth we are using ColdFusion 6 at present time, but will be upgrading to 8 in the near future.
First of all, are you willing to have the index.cfm in the URL? Like: http://www.mysite.com/index.cfm/level-1/level-2/level-3/level-4 ? If not, then you'll need to be doing a rewrite to remove the index.cfm, but still allow CF to process the page. Your .htaccess would look something like this:
RewriteEngine On
# If it's a real path, just serve it
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule . - [L]
# Redirect if no trailing slash
RewriteRule ^(.+[^/])$ $1/ [R=301,L]
# Rewrite URL paths
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-d
RewriteRule ^([a-zA-Z0-9/-]+)$ /index.cfm%{REQUEST_URI} [PT]
Next step, you'll need to "catch" the URLs and serve up the correct pages based on the SEO-friendly URLs. You can grab the incoming URL from the CGI.path_info variable. It's hard to know what your code should look like without knowing how it currently processes those URL variables, but essentially you'd have some kind of mapping function that grabbed the SEO-friendly names and substituted in the numbers to grab the content.
The third step is rewriting any URLs that are generated by your CMS to output the SEO-friendly URLs. Same mapping happens here, only in reverse.

Resources