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

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]

Related

URL Rewritng is working but auto redirect is not for my website

I have used URL rewriting with .htaccess with the below code. Problem is that if we paste the URL then it's working but auto redirection is not working.
.HTACCESS CODE:
Options +FollowSymLinks
RewriteEngine on
RewriteRule Sports-Equipment-Catname-(.*)\.html$ Sports-Equipment.php?Catname=$1
My URLS:
http://example.com/Sports-Equipment.php?Catname=Discus
If I paste direct URL from htaccess is:
http://example.com/Sports-Equipment-Catname-Discus.html
why URLs is not auto-redirected to new pages.
Please help me to fix it.
why URLs is not auto-redirected to new pages.
There is no "automatic" process here. You have no directives that implement the redirect.
My URLs: /Sports-Equipment.php?Catname=Discus
That's the problem, you need to change the actual URLs in your HTML source. This should not be resolved with a "redirect". Otherwise, the user can still "see" the old URL before they click the link. If they copy a link they are copying the "old" URL. Every time the user clicks a link they are redirected - which is slow, not just for the user, but also for your server (uses more resources).
However, you should implement a redirect if you are changing an existing URL structure. But this is to preserve SEO only. For search engines that have indexed the old URLs and for third party backlinks to the old URLs.
For that you would need to add the following "redirect" before the existing rewrites. For example:
# Redirect "old" URL to "new" URL
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^Catname=([^&]*)
RewriteRule ^Sports-Equipment\.php$ /Sports-Equipment-Catname-%1.html [QSD,R=301,L]
The condition that checks against the REDIRECT_STATUS env var ensures we only redirect direct requests and not rewritten requests by the later rewrite - thus avoiding a redirect loop.
The QSD flag discards the query string from the initial request.

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. :)

Mobile Redirect using htaccess

My site is:
example.com
My mobile version:
m.example.com
Profile page is
example.com/profile or m.example.com/profile
Or posting page
example.com/posts or m.example.com/posts
Mobile version's url is similar. So, how to redirect the same page?
For example: user go (from facebook) into example.com/posts but he uses mobile device,
so how to redirect via .htaccess to m.example.com/posts
I saw this one threat, but i'm confuse to create right rules.
Thanks :)
Your question is similar to this question, except that you want to redirect the user. First you need a list of mobile user-agents. I am not going to suggest a list, but I think you should be able to find a list that is suitable for you.
Next, you want to go to your .htaccess file and add something like this:
RewriteCond %{HTTP_USER_AGENT} ^(user-agent1|user-agent2|user-agent3|etc)$
RewriteCond %{HTTP_HOST} !^m\.example\.com$
RewriteRule ^(.*)$ http://m.example.com/$1 [R,L]
The first RewriteCond contains all user-agents you want to redirect. Remember that it is a regex, so you have to escape any special characters! The second RewriteCond checks if the host isn't already m.example.com. If we wouldn't check for this, we would create an infinite loop, redirecting to the same url over and over. The RewriteRule itself matches everything after the hostname (and before the query string) and redirects it to the mobile site. $1 gets replaced with the first capture group in the regex in the first argument of RewriteRule. The [R] flag is not necessary in this case (links with http:// will always be redirected), but it let's Apache know that it should do an external temporary redirect to the new url. The [L] flag will stop processing more rules if this rule matches.
See the documentation for more information.

Multiple htaccess re-write rules with friendly URLs

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?

Categories