Multiple htaccess re-write rules with friendly URLs - php

My mobile site (tiny custom CMS inspired by wordpress) has a homepage, blog page, a news page, an about page and a contact page where ./blog ./news ./about and ./contact are actual folders. Homepage, Blog and News are paginated. About and Contact are single pages, no pagination.
I need htaccess rewrite rules to do the following (A) and (B). I have only made progress with (A), and the htaccess file is in the site root.
(A)
m.website.com/index.php?page=1 re-writes to m.website.com
m.website.com/index.php?page=2 re-writes to m.website.com/page/2 etc
m.website.com/blog/index.php?page=1 rewrites to m.website.com/blog
m.website.com/blog/index.php?page=2 re-writes to m.website.com/blog/page/2 etc
m.website.com/news/index.php?page=1 rewrites to m.website.com/news
m.website.com/news/index.php?page=2 re-writes to m.website.com/news/page/2 etc
Problems:
Below is what I'm using for the above, but I only got it working for the homepage for now. I don't know how to combine the rules to include blog and news pages too. Also, it duplicates my links because m.website.com and m.website.com/page/1 are both in use. I need to get rid of /page/1 everywhere. Pagination should start only from page 2. I tried to get rid of it using the RedirectMatch but it didn't work so I commented it out.
RewriteEngine On
RewriteBase /
RewriteRule ^page/(.*) index.php?page=$1 [NC]
#RedirectMatch 301 ^/page/1/$ http://m.website.com/
(B)
I already have a permalink.php file which accepts pretty URLs and returns their postIDs
The read-more link for each article on the home, blog or news page will have the format http://m.website.com/2012/03/the-post-title
When clicked, the htaccess will query permalink.php with the string /2012/03/the-post-title to get the postID, then opens http://m.website.com/article.php?id=postID but the address in the address bar will be http://m.website.com/2012/03/the-post-title and this shows the article in full, be it home page, blog page or news page.
Problem: I have been searching and I'm not exactly sure how to go about (B) above but I know it's possible. In the end, all rules for A and B will be in the same htaccess file in the site root.
Thanks

This should do the trick for you. It looks like you have done most of it so there isn't muc to do.
RewriteEngine On
RewriteBase /
# you do not need a rewrite for the root as you should have index.php
# as your default document and page 1 as your default page
RewriteRule ^page/1/?$ / [NC,L,R=301]
RewriteRule ^page/(\d+)/?$ index.php?page=$1 [NC,L]
# you do not need a rewrite for blog as you should have index.php as your
# default document and page 1 as your default page
# you do not need a rewrite for news as you should have index.php as your
# default document and page 1 as your default page
RewriteRule ^(blog|news)/page/1/?$ $1 [NC,L,R=301]
RewriteRule ^(blog|news)/page/(\d+)/?$ $1/index.php?page=$2 [NC,L]
######################################################################
################## ADD YOUR PERMALINK.PHP CODE HERE ##################
######################################################################
UPDATE
To effectively turn /2012/03/the-post-title into a postID you need to be able to ask your database to do that for you as it is the only thing that knows the answer. So you can either use a RewriteMap http://httpd.apache.org/docs/2.3/rewrite/rewritemap.html#dbd I have never done this myself and I would advise against it. Not because I know there is a problem with it I just have a bad feeling about it.
The alternative and one I would champion is to just do something like this:-
RewriteRule ^(\d{4})/(\d{2})/([^/]+))/?$ article.php?postIdentifier=$1/$2/$3 [L]
Then in article.php hit the database and use the information in the postIdentifier to get the correct article.
Make sense?

Related

htaccess rewrite rule for affiliate id

My site is just index.php and every page is read from that. I'm not using WordPress.
What I'm seeking to do is the same thing that WordPress does with its page structure. In Wordpress you can set the rewrite rule so that a subsequent subdirectory can pass a value to index.php.
I have a client that is building an affiliate network and the base URL is for example:
mywebsite.com/?id=viktor
They would like to parse URL's instead so that it's much simpler for the customers.
i.e. mywebsite.com/u/viktor would 'rewrite' to mywebsite.com/?id=viktor.
What would be the .htaccess rule or associated PHP I'd have to put in index.php?
Obviously we won't have a 'u' folder in the site. :)
Thanks for your help.
This related question I believe has the code we're looking for however I'm not sure how it would apply in this situation
You can do it like this:
RewriteEngine On
RewriteRule ^u/(.+)$ /?id=$1 [NE,L]
If you need to also preserve any existing query string (like /u/viktor?param=value), use this:
RewriteEngine On
RewriteRule ^u/(.+)$ /?id=$1 [NE,QSA,L]
Update
Well it's bad practice using relative URLs (for this reason), but if you really want to avoid updating all your code, which could easily be done with a script or a command, you could do this:
RewriteEngine On
RewriteRule ^u/(.+/.+) /$1 [NE,L]
RewriteRule ^u/(.+)$ /?id=$1 [NE,QSA,L]
Which would rewrite everything that is referenced in a folder under /u/ to root. But it breaks browser caching as all your resources now have two URLs. Really you should just update your URLs to absolute.
If the above doesn't work for you and you only want to rewrite specific folders, do it like this:
RewriteEngine On
RewriteRule ^u/((?:assets|images|css)/.+) /$1 [NE,L]
RewriteRule ^u/(.+)$ /?id=$1 [NE,QSA,L]
Replacing your folder names as appropriate.
The problem you have here, Viktor, is that WordPress checks the original URL when it is processing a request. This is because it has its own mod_rewrite rules to rewrite everything to index.php.
So you can't do what you want with mod_rewrite, because WordPress won't look at your rewritten URL, and will process it as /u/viktor. You can do it with a 301 redirect, but you already said you don't want to do that.
In order to make this work properly, you need to work with WordPress to make those URLs work, perhaps creating a plug-in to do it. I wouldn't suggest adding anything to WordPress's index.php as it will only get overwritten when WordPress is updated.

htaccess redirect to remove duplicate pages

I'm very new to this thing with htaccess rules.
I basically want two things.
Redirect exampledomain.com to www.exampledomain.com. This works, the web host that is used had a FAQ page where they suggested the code I use. What doesn't work is this:
Remove the way to access the "example" page from both "/index.php?p=example" and "/?p=example" to get better search engine ranks. If this means accessing that page from something like "/example", that's a bonus.
I basically want to remove the duplicate links that is caused by the /index.php-variant to get higher search engine ranks. Having prettier links isn't required, just a bonus, the most important thing is that I need to remove the duplicate links.
Here's the code anyway:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^exampledomain.com$
RewriteRule ^(.*)$ http://www.exampledomain.com$1 [R=301]
RewriteRule ^([^/]*)\.html$ /index.php?p=$1 [L]
RewriteRule ^([^/]*)\.html$ /?p=$1 [L]
Have I misunderstood the [L/R=301/*]-flags totally?
EDIT: Ok, to clarify. I want to remove duplicate urls to get higher search engine ranks. What my code above apparently did was to add yet another url to access the same page. Since I only want one way, is there any way to redirect instead of just pointing the "prettified" link to the original?
When I think about it, would a redirection of exampledomain.com/index.php to exampledomain.com do the trick?
Thanks guys so far, sorry for being bad at explaining. :)

Subdomain, htaccess Rewrite and masking URL

Users on my site are creating their own pages, i want to make subdomain with redirect and masking real URL.
I have made RewriteRule:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(.+)\.domain\.com$
RewriteCond %1 !=www
RewriteRule ^(.*)$ http://domain.com/site.php?site=%1 [QSA,R=302]
When user enters:subdomain.domain.com he will be trasfered to php script witch finds ID by page name in DB and redirects to:
domain.com/workarea/pages/ID/index.html
I wan't to mask real URL, just to show the URL with subdomain
subdomain.domain.com
but also i want to show all after / when user go to gallery or smth else, like:
subdomain.domain.com/index.html
subdomain.domain.com/gallery.html
but i don`t know how to do that.
I've tried to use [P] flag, but it just redirects to domain.com,
or it is just don`t let the php script to work.
Please help me!
First off, even if set up to have htaccess redirect a subdomain to php; the following remark:
When user enters:subdomain.domain.com he will be trasfered to php
script witch finds ID by page name in DB and redirects to:
domain.com/workarea/pages/ID/index.html
Will ensure that your visitors will see domain.com/workarea/pages/ID/index.html, because - as you say - php redirects visitors to that page. If you want to show the htaccess "masked" url, you need to make sure PHP parses the required HTML and not redirect.
As to the "masked" redirection from htaccess. What is the current behavior?

How do you let all the sub pages to the main page?

How do you let all the sub pages to the main page?
sampel.com/2/sampel.html
so
sampel.com/sampel.html
so all sub pages move to the main page
wear .htaccess
How do you let all the sub pages to the main page?
Here is you do that via mod_rewrite rules:
RewriteEngine on
RewriteRule ^[^/]+/([^.]+\.html)$ /$1 [L,NC,R=302]
RewriteRule ^([^.]+)\.html$ /pages/head.php?cari=$1 [L,NC]
This will redirect every /dir/file.html to /file.html
Please read the documentation and search for examples.
I am not sure if you want to have an external redirect, or an internal rewrite. For the rule I show below, I am assuming you want the url in the browser to change, and therefore want an external redirect. First you need to make sure that mod_rewrite is enabled in your apache install. Then enable the rewriteengine in your .htaccess file and write the rules below it. You'll need to use "RewriteRule". The first argument is a regex that matches the request. The second argument is what it is rewritten to. The third arguments are a list of flags. You can find information about them in the documentation linked above. You want to make sure the first argument doesn't match the part that it is rewritten to, or you'll create an infinite loop, eventually causing an error somewhere. The following rule should do what you want it to do.
RewriteEngine on
RewriteRule ^2/(.*)$ $1 [R,L]

Very advanced .htaccess

Okay, I've looked around a bit, and I cannot seem to find the answer.
Now I'm not asking for you to give me just a block of code, and say "this is the fix", I would like for you to explain how you did it, what you came up with, etc. I would really like to know more about this, and the only way to learn is by example.
Now what I need to do is:
Currently my blog (word press go figure, cause they own the world), is on the root directory. So my posts look like: http://localhost/hello-world. Just an example.
However, I've just extended my site, with a custom built PHP script using Code Igniter. I need to put the Code Igniter script on the root directory, and move the blog to /main/ or /blog/. I don't want to get rid of my blog, but I don't want Google to have to re-index every single blog post there is, or have Google lead to bad post urls. More importantly I don't want to have to sit here for hours and hours, creating redirect urls to each and every single blog post (theres hundreds of them).
Now I need the redirect to be a 301 redirect.
Heres the problem I have come across.
I NEED to have this in the .htaccess:
RewriteEngine on
RewriteCond $1 !^(index\.php|admin|css|img|js|main|favicon.ico)
RewriteRule ^(.*)$ index.php/$1 [L]
This removes the ugly localhost/index.php/controller/controller-function/
But I need to make my .htaccess redirect everything on the / but NOT the preset urls I already have.
The Preset url arguements I have through Code Igniter are:
/details/
/register/
/city/
/search_process/
/login/
/logout-success/
/login-success/
/logout/
/login/
/manage/
/panel/
So in essence..
localhost/hello-world/ would have to 301 redirect to localhost/main/hello-world
and
localhost/(any-of-the-above)/ would have to NOT redirect to /main/ or /blog/
Also if you notice in the current .htaccess, I've allowed certain things like /admin/ /css/ /img/ /js/ (/main/ is the blog obviously) and favicon.ico (cause it looks awesome)
Please school me! (How often do you hear that? :P)
CREDIT GOES TO LAWRENCE CHERONE!
He originally gave me the answer, and between then and now I think I made a typo the first time, and lost the code by accident, and it didn't work the second time after copy / pasting (cause the first time i actually typed it out to learn it).
So here goes with the modified working solution. I give full credit to Lawrence Cherone (the guy who has the accepted answer for this question) because he helped me figure this out, and ultimately I couldn't have gotten this working solution without him.. Thanks again bud!
RewriteEngine on
RewriteCond $1 !(panel|manage)
RewriteRule ^(.*)$ http://localhost/choosefitness/main/$1 [R=301,L]
RewriteEngine on
RewriteCond $1 !^(index\.php|admin|css|img|js|less|favicon.ico)
RewriteRule ^(.*)$ index.php/$1 [L]
This simply states, that as long as the first argument http://localhost/choosefitness/first-argument is no /panel or /manage. It should 301 redirect to /main/ (Had to provide full URL for 301 redirect, not just main/$1)
Then it states that any request made on admin css img js lss or favicon.ico should be ignored. else should be made on index.php (this removes the index.php from the url in code igniter)
I do not know why it works i just know it does. However I havent tested it fully but I believe to be able to access /css without beign redirected, you have to add |css to the first RewriteCond. However the server is able to access the css files without needing to do so.
This is untested & pretty sure index.php/$1/$2 is wrong but... You could include a rule that matches your CI rules and then pass to the CI controller, and if not match is found then rewrite to your /blog/ url. Also dont forget to escape the . in the favicon\.ico part.
RewriteEngine on
RewriteCond $1 !^(index\.php|admin|css|img|js|main|favicon\.ico)
RewriteRule ^(details|register|city|search_process|login|logout-success|login-success|logout|login|manage|panel)/(.*)$ index.php/$1/$2 [L]
RewriteRule ^(.*)$ /blog/$1 [L]
Edit: If your parameter is at the front then switch the rule to something like this:
RewriteRule ^(.*)/(details|register|city|search_process|login|logout-success|login-success|logout|login|manage|panel)$ index.php/$1/$2 [L]

Categories