clean url with .htaccess but with only one url - php

i'm using .htaccess to create clean urls.
here is a piece of .htaccess file:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(/$|\.)
RewriteRule (.*) %{REQUEST_URI}/ [R=301,L]
RewriteRule ^posts/page/([0-9]+)/$ posts.php?page=$1
RewriteRule ^posts posts.php
it works fine but problem is both urls work!
is there a way to only make site.com/posts/ work and return 'not found' on site.com/posts.php(or redirect to site.com/posts/)
think i read somewhere two urls for the same page is bad for seo.

You can create a rewrite rule that returns 404 HTTP code on every ".php" URL :
RewriteRule .*\.php$ - [R=404]

This is working fine for me. Hope it will work fine for you as well.
RewriteEngine On
Options -Multiviews
#For rewriting request to posts.php
RewriteCond %{REQUEST_URI} ^/posts/?$
RewriteRule .* /posts.php [L,END]
#For returning 404 on directly accessing /posts.php
RewriteCond %{REQUEST_URI} ^/posts.php$
RewriteRule .* - [R=404,L]

To redirect /posts.php to /posts you can use this
RewriteEngine on
RewriteCond %{THE_REQUEST} /posts\.php [NC]
RewriteRule ^ /posts [NE,L,R]
#Your other rules
If you want to redirect the orignal request to 404 , change the RewriteRule line to this
RewriteRule ^ - [R=404,L]

Related

.htaccess 301 redirect and remove page.php

So my problem is that I have this running in a sub-directory and there are a couple problems...
When I add the 301 redirect I get a 500 error
I cannot get "page" removed from the URL (http://174.136.15.245/~clubz/test/page/about-club-z-tutoring-test)
Here is my code:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTPS} !=on
# RewriteRule (.*) http://174.136.15.245/~clubztutors/test/ [R,L]
RewriteBase /~clubz/test/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# RewriteRule ^(.*)$ page.php?/$1 [R=301, L]
# RewriteRule ^page.php?p=$1 page/([^/.]+)/?$ [R=301, L]
# This works but add "page" directory
RewriteRule ^page/([^/.]+)/?$ page.php?p=$1 [L]
</IfModule>
I did look through some other solutions here but I still could not get this to function exactly the way I needed. Any insight would be appreciated.
Jackpot! Well, at least on getting page removed from the URL:
RewriteRule ^([^/.]+)/?$ page.php?p=$1 [L]
The redirect still doesn't work but it would be nice to have. For now I'll run with this.

Remove/Redirect index.php from url to prevent duplicate urls

Please read the question carefully before marking as duplicate.
We all know, that using in .htaccess:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
we can redirect all traffic to index.php so we can create friendly urls and have one front controller.
Although the question is connected to mod_rewrite the problem is described for Laravel.
The following .htaccess comes by default with Laravel 4 and it works fine:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
If we run url mydomain.com/something and have set that route for something properly, some controller will be launched. It works fine so far.
However in Laravel 4 we will be able to reach the same route using mydomain.com/index.php/something. Probably using Laravel url creating we will have no urls with index.php in url but there is some other problem.
For example if our competition would like to make us some harm, they can simple put in Internet single links for urls to mydomain.com/index.php/something, mydomain.com/index.php/something2 and so on and search engines will see duplicate urls.
Of course if we have our custom PHP application, we can do it in PHP without a problem checking simply $_SERVER['REQUEST_URI'] and make 301 redirection. We can of course do the same in Laravel but we have to write this code in PHP each time and probably some developers could say it is bad practice to do it in PHP.
Question is simple: how can I redirect in .htaccess all urls that contain index.php to to the same url without index.php?
Example urls that should be redirected:
mydomain.com/index.php/something should be redirected to mydomain.com/something (something could be anything - can contain any characters)
mydomain.com/index.php should be redirected to mydomain.com
mydomain.com/index.php?anything should be redirected to mydomain.com (anything can contain any characters)
mydomain.com/index.phpanything should be redirected to mydomain.com anything can contain any characters)
Insert these rules just below RewriteEngine On line:
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*?)index\.php[^/] /$1? [L,R=302,NC,NE]
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*?)index\.php(?:/(.*))?$ /$1$2? [L,R=302,NC,NE]
After spending hours I write below code for me and its 100% working
Redirect index.php to non index.php
RewriteCond %{THE_REQUEST} ^.*/index\.php
RewriteRule ^index.php/(.*)$ /$1 [R=301,L]
how can I redirect in .htaccess all urls that contain index.php to to
the same url without index.php?
Add this to your .htaccess
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
RewriteRule ^ %1 [R=301,L]
For Nginx, here is the rules :
location / {
rewrite ^/(.*?)index\.php[^/] /$1? redirect;
rewrite ^/(.*?)index\.php(?:/(.*))?$ /$1$2? redirect;
}
This solved my problem to force https & remove index.php from the url in Kohan 2.3
RewriteEngine On
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*?)index\.php[^/] /$1? [L,R=302,NC,NE]
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*?)index\.php(?:/(.*))?$ /$1$2? [L,R=302,NC,NE]
RewriteRule ^(application|system) - [F,L]
RewriteCond %{THE_REQUEST} /index.php [NC]
RewriteRule ^(.*)index\.php$ /$1/ [R=301,L,NC,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?!localhost$|127\.0\.0\.1$)(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]

how can i manage url using .htaccess in php?

i want to manage url of my website
right now its showing me www.computermall.co.in/product_details.php?prdid=34
but i want www.computermall.co.in/product_details/34
how can i do it?
i have tried this
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /computermall
# Get the URI-path directly from THE_REQUEST variable
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\s/(.*)\.php [NC]
# Strip the extension and redirect permanently
RewriteRule .* /%2 [R=301,L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !\.php [NC]
# Map internally to the original resource
RewriteRule ^(.*)$ $1.php [L,QSA]
Try this:
RewriteEngine On
RewriteRule ^product_details/prdid/([^/]*)$ /product_details?prdid=$1 [L]
Edit: removed leading slash
First of all your RewriteBase /computermall is confusing but i am assuming thats there because your domain is in a sub directory of another active domain/http server path.
Here is your htaccess
RewriteBase /
RewriteRule ^computermall/product_details/([^/]*)/([^/]*$ /computermall/product_details?prdid=$2 [R=301,L]

URL Rewrite for multiple pages but same query

As of now I have one generic query "view" which I'll use on most pages...
My rewrite is:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/?$ $1.php?view=$2 [L]
My url is:
classroom.php?view=creation
I want it to look like:
classroom/creation
I have it working but it screwed up other urls
dashboard.php was just /dashboard
but broke when I did the first rewrite rule...
These things are a pain to understand.
My Question is how can i have /dashboard, /classroom, /classroom/creation work?
EDIT: I got it working with:
RewriteRule ^([a-zA-Z0-9_-]+)$ $1.php [L]
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/?$ $1.php?view=$2 [L]
The first 2 rules are for external redirect, which turns .php into a directory like format rest of the rules are for the internal redirect which keeps the pretty URL and doesn't show the redirect.
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
# Redirect /classroom.php?view=creation to /classroom/creation
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+classroom\.php\?view=([^\s]+) [NC]
RewriteRule ^ /classroom/%1? [R=302,L]
# Redirect /classroom.php to /classroom and /dashboard.php to /dashboard
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(classroom|dashboard)\.php [NC]
RewriteRule ^ /%1 [R=302,L]
# Internally forward /classroom/creation to /classroom.php?view=creation
RewriteRule ^classroom/(.*)/?$ /classroom.php?view=$1 [L,QSA,NC]
# Internally forward /classroom to /classroom.php and /dashboard to /dashboard.php
RewriteRule ^(classroom|dashboard)/?$ /$1.php [L,QSA,NC]

Simple htaccess rewrite/redirect to search page

I have a search form that sends a GET request to a page called search.php. I have rewrite rules set up in my htaccess file that rewrite certain things, like /search, to their respective pages. I simply want to take the search.php?q=query and rewrite it to /search/query.
Here is what I have.
RewriteRule search.php?q=(.*) /search/$1
RewriteRule search/(.*) search.php?q=$1 [nc]
What am I doing wrong?!
Here is the complete file
ErrorDocument 404 /index.php?p=404
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^images/.*\.jpg$ /images/default.jpg [L]
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?wghandcrafted.com/.*$ [NC]
RewriteRule \.(gif|jpg|jpeg|psd|js|swf|flv|png)$ /feed/ [R=302]
RewriteRule ^(products|blog|feed|search|checkout|checkout)$ $1.php [nc]
RewriteRule products/cat/(.*)$ products.php?type=cat&cat=$1 [nc]
RewriteRule products/(.*)$ products.php?type=single&product=$1 [nc]
RewriteRule blog/(.*) blog.php&post=$1 [nc]
RewriteRule feed/(.*) feed.phptype=$1 [nc]
RewriteRule search\.php?q=(.*)$ /search/$1 [R=301,L]
RewriteRule search/(.*)$ search.php?q=$1 [NC]
Make the first line perform a Redirect and the second perform a Rewrite
RewriteRule search\.php?q=(.*)$ /search/$1 [R=301,L]
RewriteRule search/(.*)$ search.php?q=$1 [NC]
and move
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^images/.*\.jpg$ /images/default.jpg [L]
to the end of the set of rules.
Otherwise, the RewriteCond %{REQUEST_FILENAME} !-f rule is enacted before anything else, meaning that only requests for non-existant files will be handled by any rules below that line. As there is a search.php file, this prevents that rule from ever being reached.
I was having the same problem and here is a solution i found that worked for me, on my site the queries are being sent to index.php, I discovered if I had "^index.php$ /search/%1? [R=301]" as the first rewrite rule it will just error out because of the second rewrite rule making it just go in a loop so i replaced "^index.php$" with "^$" allowing it to still request the same file. It might not be the best solution, but a work around that works. Here is my working code:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{QUERY_STRING} ^q=(.*)$
RewriteRule ^$ /search/%1? [R=301]
RewriteBase /search
RewriteRule ^search/(.*)$ /index.php?q=$1 [NC]

Categories