I have a website developed in PHP. I have recently done the URL rewriting which works fine. However, I just found out that my pages with parameters are also accessible. For eg.
I converted this URL
domainname.com/index.php?page=product&pid=5&proTitle=Samsung Galaxy
After rewrite it looks like this
domainname.com/products/5/Samsung-Galaxy.html
Everything works just fine. However, My site is still accessible using the old parameters. I want if someone types in the old URL should be automatically redirected to Ideally New Page if not then index page. Google and MSN shouldn't access these pages with parameter. Any help will be highly appreciated.
Thanks for your input. Here is more detail to my question.
I converted this URL
domainname.com/index.php?page=product&pid=5&proTitle=Samsung Galaxy
After rewrite it looks like this
domainname.com/products/5/Samsung-Galaxy.html
The code looks like this.
RewriteRule ^products/(.*)/(.*).html$ index.php?page=product&pid=$1&proTitle=$2 [nc]
Rewrite Works fine. However, if I try to access old URL i.e domainname.com/index.php?page=product&pid=5&proTitle=Samsung Galaxy the page is still accessible and on top of that being crawled by Google and other search engines. I want If someone tries to access this URL, it should direct them to Page Not Found and this should also not be sniffed by any crawlers.
Thanks a lot again for your time and I hope I can get your valuable reply soon.
You need another rule which redirects the browser to the nicer looking URL if the request is made for the ugly looking one. For example:
# need to replace spaces with "-"
RewriteRule ^(.*)\ (.*)$ /$1-$2 [L,R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?page=([^&\ ]+)&pid=([^&\ ]+)&proTitle=([^&\ ]+)
RewriteRule ^ /%1/%2/%3 [L,R=301]
Related
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. :)
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.
appreciate help in advance. I'm using mod rewrite and need help in converting specific PHP URLS into prettier URLs.
Specifically, I want to convert the PHP URL:
www.sample.com/subcategory.php?subcategory=sports-achievement&page=1
to
www.sample.com/sports/sports-achievement.html
for the user.
So to be specific, the rule is:
a) the first part of the subategory before the dash should be the
directory (e.g., sports- - into /sports/
b) the entire subcategory as
the page string "sports-achievement.html
c) the PHP pages still needs
to pass the page strings (as there are multiple pages)
Thanks in advance for your help!
To get the the redirect and internal rewrite, you'll have to do something like this.
#Redirect
RewriteCond %{THE_REQUEST} (GET|POST)\ /subcategory\.php\?subcategory=([^-]+)-([^&]+)(&(.*)|)\ HTTP
RewriteRule ^subcategory\.php$ /%2/%2-%3.html?%5 [R,L]
#Internal rewrite
RewriteRule ^(.*)/\1-(.*)\.html$ subcategory.php?subcategory=$1-$2 [QSA,L]
There is no real way of hiding the page name. You can use a post-request and hide it in the post-body. My answer to this question shows how to do it with cookies, but this will most likely confuse search engines, as different pages are somehow available under the same page name.
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]
Basically, I wan't to redirect images that people click on to a specific URL, as an example.
http://mydomain.com/upload/39248928434.jpg
once that image is clicked from a forum or where ever somebody linked it, I want it to return to this URL instead of the above.
http://mydomain.com/phpscript.php?image=39248928434.jpg
Thanks.
Assuming that the image will exist at http://mydomain.com/upload/39248928434.jpg, and by "clicking" on the image the intention is that their browser should be directed to that address...
I haven't tested this code out, but here's a slight modification to some code I found (here) to disable hot linking using mod_rewrite that may do what you're looking for:
RewriteEngine on
RewriteCond %{HTTP_REFERER} ^$
RewriteCond %{HTTP_REFERER} ^http://(www.)?mydomain.com/.*$ [NC]
RewriteRule upload/(.*\.jpg)$ /phpscript?image=$1
I'm not entirely sure that that gets you 100% there, as I'm not sure how the referrer is recorded when they click through from some other site and land on your page. But this should point you in the right direction at least.
Edit: removed the [F] flag, as it returns a "Forbidden" response