I have a slight problem with .htaccess redirect.
I have a dynamic site with 2 levels of variables - content="type -(alpha)" and ID="number" but this is very not seo friendly what I really would like to create is a rewrite rule that generates a "friendly" url for serach engines & users alike. Very much like WordPress does.
Each ID is already unique (obviously) and on creation is creating a unique "permalink" field so for example ID=1 has a "permalink field" as "2009/10/27/page title" and ID=100 would be "2010/10/27 page title".
I would like folder/wall.php?content=type&ID=number to redirect to folder/permalink.php/html/htm (don't mind a non dynamic extension)
Any clues? - this is not right (I know) but it also "breaks" my css file
RewriteEngine On
RewriteRule wall/content/(.*)/ID/(.*)/ wall.php?content=$1&ID=$2
RewriteRule wall/content/(.*)/ID/(.*) wall.php?content=$1&ID=$2
You need to make sure you have a URL convention that won't conflict with other files on the server. For example, if your page is displaying "Products" and you don't have an actual folder in the root of your server called /products/, you could do this
RewriteEngine On
RewriteRule ^products/([^/\.]+)/([^/\.]+)/?$ wall.php?content=$1&id=$2 [L,NC,QSA]
Replace "products" in this example with whatever name is most appropriate for what your wall.php page does.
This would equate to the following URL
www.yoursite.com/products/title/29/
Which would rewrite to
www.yoursite.com/wall.php?content=title&id=29
Related
What I have
http://www.my-domain.com/my-product1/
http://www.my-domain.com/another-product/
http://www.my-domain.com/about-us/
What I want
http://www.my-domain.com/product/my-product1/
http://www.my-domain.com/product/another-product/
http://www.my-domain.com/about-us/
The problems
I have about 100+ products.
The products needs to be redirected to the same URL prefixed with a "folder" called "product". The two first are products.
I need to exclude pages like "about-us" and others like it.
The redirect should be 301.
Question
How is it done? Would be too long to list redirecting every product manually, I think.
What I have found out so far
This might be a good start.
http://www.brightcherry.co.uk/scribbles/htaccess-301-redirect-an-entire-directory-folder/
RewriteEngine on
RewriteBase /
RewriteRule ^old_dir/(.*) http://www.example.com/new_dir/ [R=301,L]
I'm building a new site using Joomla and I've selected 'Search Engine Friendly URLs' and 'Use URL rewriting' in the Global Configuration which gives good SEF URLs but not quite perfect!
If a link to a page doesn't have a menu item associated to it the URL would look like this:
example.com/10-category/5-article
I want to remove the numbers and the hyphen using htaccess so it looks like:
example.com/category/article
I've made Rewrite Rule's in my htaccess file that looks like this:
RewriteRule ^([0-9]+)-(.*)/([0-9]+)-(.*)$ /$1$2/$3$4 [R=301,L]
RewriteRule ^([0-9]+)(.*)/([0-9]+)(.*)$ /$2/$4 [R=301,L]
The browsers address bar now shows the URL I want, example.com/category/article but the page shows a 404 error!
Is it something to do with the Joomla SEF?
What am I doing wrong?
*Update*
The first RewriteRule which removes the hyphen only works OK by itself, I only receive the 404 error page when both RewriteRule's are active.
This is a blind guess, Joomla probably needs those numbers to know which content to serve. It cannot just tell by the name of the category or article (in fact in most of these cases you can even leave it out), but it's the number that's important.
So when you're rewriting the URL without the numbers, you're requesting pages that Joomla has no idea how to handle, and it'll give you a 404. The only solution would be to write a plugin or something that maps the names of categories and articles to the corresponding IDs, but that's not going to be easy.
Concerning SEO, I don't think the number in the url is that much of a negative effect. If the rest of your website's SEO is good then this won't matter.
I am really new to .htaccess. I was wondering how to create a complex seo friendly urls for the search string and selected filters.
I'm using following code currently for the search.
ReWriteRule ^search/(.*)/(.*) ?module=search&q=$1&page=$2 [L]
ReWriteRule ^search/(.*) ?module=search&q=$1 [L]
When it comes to adding filter options it starts to be a nightmare. Consider following filters;
Short by: none, name, date + ASC, DESC.
Category: none, category ID.
Search In: All, title, Message, Author.
When all filters are selected our address would be;
Raw :
www.site.com/?module=search&q=test&shortBy=name_ASC&catID=1&searchIn=title
Seo Friendly : www.site.com/search/test/name_ASC/1/title/
Now that's not complex at all but I just want to understand the how things work.
Do I have to apply all the filters to URL even if they are not selected? This would create longer URLs for no reason so I believe there must be another approach to this.
How do I define the rule in .htaccess? As in my example, I am writing at least 2 rules in my .htaccess file (2nd one is for pagination).
All I could think right now to do something like this ^search/(.*)/(.*) ?module=search&$1=$2 [L] but this doesn't look elegant.
I will be glad if you could help me out with this problem. I will be grateful if you could also share some of your experiences to a .htaccess newbie like I am.
Put the rules below at the top of your .htaccess file (in the root directory of your site) and then select one of the options below, modify it to your needs and place in after in your .htaccess file.
The first option will match the most urls and is not recommended. The second will only match urls with 5 directories, but requires all the paramters, so I recommend the 3rd.
Though the pattern below may not look elegant, it is a featured solution in The Definitive Guide to Apache mod_rewrite
For more details look at the apache docs or some examples or this post
In all cases, I am assuming that the requests go to index.php, so you should modify this to match the actual page.
This section should go at top of .htaccess file for all 3 options
#for all 3 options these 2 lines should be at the top of the .htaccess file
RewriteEngine On
RewriteBase /
Option1
#1 if all parameters are optional, and you page (index.php) can handle blank parameters use
RewriteRule ^([^/]*)/?([^/]*)/?([^/]*)/?([^/]*)/?([^/]*)/$ index.php?module=$1&q=$2&shortBy=$3&catID=$4&searchIn=$5 [L]
Option2
#2 if all parameters are required use
RewriteRule ^([^/]+)/([^/]+)/([^/]+)?([^/]+)/([^/]+)/$ index.php?module=$1&q=$2&shortBy=$3&catID=$4&searchIn=$5 [L]
Option3
#3 if the module is required and other parameters are optional, then this is better
RewriteCond %{REQUEST_URI} ^/(search|produce_detail|other_modules_here)/
RewriteRule ^([^/]*)/?([^/]*)/?([^/]*)/?([^/]*)/$ index.php?module=%1&q=$1&shortBy=$2&catID=$3&searchIn=$4 [L]
I have a website running on ExpressionEngine and a custom backoffice based on Zend.
Both approaches expect a specific htaccess file placed in the root.
This is my structure.
http://localhost/... (ExpressionEngine pages, eg. http://localhost/calendar/events)
http://localhost/backoffice (Backoffice pages)
Before placing the backoffice on a subdomain, I wanted to know if it's possibile to have one htacces file and to apply a different set of htaccess rules based on the uri. So eg. everything with "backoffice" gets different rules.
I know you can do stuff like this:
Best I've found was the following Can I do an if/then/else in htaccess?
But I'm stuck with defined the variables based on the uri.
Thanks for your help.
regards
Vic
Put your rewrite rules for EE first and add a rule for the back office there:
RewriteCond %{REQUEST_URI} !/backoffice
RewriteRule ^ /index.php [L]
And now add the rules for the back office.
This is a totally new area for me so please be patient. I want to create "permalinks" for a dynamic site I am working on. At the moment all the pages (not the index) are referenced using an ID variable thus:
http://www.domainname.com/page.php?ID=122 (etc)
I want to create a suitable rewrite rule so that a useable URL would be more like this:
http://www.domainname.com/page/'pagetitle'.html (could be .php doen't matter)
Page title is stored in the database and obviously is linked directly to the ID
Am I right in thinking thr rewrite rule would be something like this?
RewriteCond %{QUERY_STRING} ^(([^&]*&)*)ID=([^&]+)(&+(.*))?$
RewriteRule ^page\.php$ /page/%3?%1%5 [L,R=301]
My ideal would be to just create
http://www.domainname.com/'pagetitle'.html
But have absolutly no idea how to do that.
Now the other question/sub question.
If the rewrite works i.e. you type in http://www.domainname.com/page/'pagetitle'.html to a browser address bar does the htaccess file work "the other way" in accessing the page http://www.domainname.com/page.php?ID=122 or do I have to create a function to take the 'pagetitle'.html bit of the URL and convert it to page.php?ID=122 ?
Also, sorry, but this is all new; if I create a site map (xml or php etc) using http://www.domainname.com/page/'pagetitle'.html will the SE spiders go to http://www.domainname.com/page.php?ID=122? or di I need to create the sitemap using the ID variables?
Question 1 and 2:
The condition is not required in this case. Use it like this:
RewriteRule ^/page/([\w-]+).html$ /page.php?title=$1 [L,R=301]
This transforms
/page/blabla.html to /page.php?title=blabla
You need to find the right page using the title parameter in page.php
Question 3:
I suggest you never use the querystring variant of the urls in any of your anchor links or xml sitemap. This way the spiders will only know of the friendly urls.