.htaccess prevent changes rewritten url - .htaccess

I have following .htaccess rewirite rule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (/m)?/([^/]+)\.(jpg|jpeg|gif|png)$
RewriteRule .* http://domain.com/user/upload%1/%2.%3
So, now link transforms (like redirect) from http://domain.com/12345.png to http://domain.com/user/upload/12345.png.
What have I to fix (I think, it should be some flag), if I want to prevent this transformation?

Remove the http://domain.com from the target of your rule:
RewriteRule .* /user/upload%1/%2.%3 [L]

Related

adding .php extension if not exist

I have the below rewrite but I want to replace the URL without extension .php if the user types the URL with .php at last, how can I revise the below rewrite script?
# /m/yyy rule
RewriteRule ^m/([\w-]+)/?$ merchants/$1/index.php [L,NC]
# /m/yyy/abc rule
RewriteRule ^m/([\w-]+)/([\w-]+)$ merchants/$1/$2.php [L,NC]
# /m/yyy/abc/ rule
RewriteRule ^m/([\w-]+)/([\w-]+)/$ merchants/$1/$2/index.php [L,NC]
With your shown samples, please have your .htaccess file with following Rules. Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
# metchants/yyy/index.php to redirect /m/yyy rule
RewriteRule ^merchants/([\w-]+)/index\.php/?$ m/$1 [R=301,L,NC]
# merchants/yyy/abc.php to redirect /m/yyy/abc rule
RewriteRule ^merchants/([\w-]+)([\w-]+)\.php/?$ m/$1/$2 [R=301,L,NC]
# merchants/yyy/xyz/abc.php to redirect /m/yyy/xyz/abc rule
RewriteRule ^merchants/([\w-]+)/([\w-]+)/index\.php/?$ m/$1/$2 [R=301,L,NC]
# To handle /m/yyy/index.php rewrite rule.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^m/([\w-]+)/?$ merchants/$1/index.php [QSA,L,NC]
# To handle /m/yyy/abc/index.php rewrite rule.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^m/([\w-]+)/([\w-]+)$ merchants/$1/$2.php [QSA,L,NC]
# To handle /m/yyy/xyz/abc/index.php rewrite rule.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^m/([\w-]+)/([\w-]+)/$ merchants/$1/$2/index.php [QSA,L,NC]

Remove duplicate content issue using htaccess

My .htaccess file looks likes this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php?pg=$1 [L]
When a non-file or non-dir url is requested, it works fine.
But if index.php?pg=(pagename) is requested, it also works which can possibly lead to duplicate content issue.
Is there a way to prevent this?
You're right, this can lead to duplicate content.
And, yes, it is possible to avoid it.
This solution redirects old url to new url equivalent, for instance http://website.com/index.php?pg=get/my/page/please.html to http://website.com/get/my/page/please.html.
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} \s/index\.php\?pg=([^&\s]*)\s [NC]
RewriteRule ^ %1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?pg=$1 [L]

Sub directory as GET with htaccess

I want to use "fake" sub-directories as GET requests. Sorry for my inaccurate wording.
I current have this in my htaccess:
RewriteEngine on
RewriteRule ^pggr/(.*)$ index.php?p=$1
So http://example.com/pggr/blah is treated as http://example.com/?p=blah.
However I want to remove the pggr part from the url, so it would be just http://example.com/blah
You can use this rule:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.+) index.php?p=$1 [L,QSA]

htaccess - URL rewrite - Remove slashes, but not from files

I use PHP.
A working htaccess-file
RewriteEngine On
RewriteBase /folder/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule .*[^/]$ $0/ [L,R=301]
RewriteCond $1 !^(index\.php)
RewriteRule ^(.+)$ index.php/$1 [L]
Look at the row with a # comment. When uncommented it adds a redirect to a slash. I use URL rewrite with toroPHP.
I want to rewrite to ending slash
I want to redirect to ending slash from rewritten URLs, just like the code above.
I don't want ending slash from real files, like jquery.js, style.css.
Example (updated 2012-12-21)
/category/test should be /category/test/
http://www.test.com/myjsfile.js should be http://www.test.com/myjsfile.js
Problem
If I use the code above uncommented it add an ending slash to all urls, including javascript files and css files.
I only want the rewritten urls to end with slash.
Question
Can it be done with htaccess? If so how?
The htaccess was almost correct. However the REQUEST_FILENAME needs to be before EVERY rewrite rule, not just before the first.
This works
RewriteEngine On
RewriteBase /folder/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .*[^/]$ $0/ [L,R=301]
RewriteCond $1 !^(index\.php)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php/$1 [L]

apache mod-rewrite

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ([0-9a-zA-Z_\-]+)(|\.html|\.php)$ page.php
this is the rule is .htacess.
what I want is if file is not found then redirect to page.php.
for example if url people.php ,people.html is not found then redirect to page.php.
because I also want those filenames without .php like people direct to people.php first. then check file exist or not, if not redirect to page.php.
how do I add one rule to make people redirect to people.php
These rules should work for you:
RewriteEngine on
Options +FollowSymlinks -MultiViews
# if there is no . in request then append .php
# it is important to make [L] as last rule here
RewriteCond %{REQUEST_FILENAME} !\.
RewriteRule ^(.+)$ /$1.php [L]
# 404 handling for files that don't exist
# NC is for ignore case comparison
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^[^.]+\.(html|php)$ /page.php [L,NC]
See if this does what you're looking for:
RewriteEngine on
RewriteRule ^(?:.+/)?([^/.]+)$ $1.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule \.(?:html|php)$ page.php

Resources