1-3 variable htaccess overriding images - .htaccess

I am currently using the below code for my .htacess file. It is supposed to re-write any http request to https as well as work for 1-3 variables (site.com/cat/subcat/ad).
That part works great, however it is also re-writing my images (i.e "/images/image.png"). How do I correct this code to prevent this from happening and allow my images to show?
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ index.php?cat=$1 [NC,L]
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?cat=$1&subcat=$2 [NC,L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ index.php?cat=$1&subcat=$2&ad=$3 [NC,L]

Related

RewriteRule in .htacces not working properly

In my .htaccess file I have several 301 redirects.
The following line works perfectly:
RewriteRule ^sub1/ https://example.com/sub1 [L,R=301]
However, if there is another / in de url, the page will not be redirected properly.
RewriteRule ^sub1/sub2/ https://www.example.com/sub [L,R=301]
This will end up at example.com/sub1/sub2/ although it loads the page of example.com/sub1 as it does nothing with the /sub2.
How can I force the correct url rewriting?
EDIT
I also have some lines to remove .php in the .htaccess that might be causing this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]

Why does echoing htaccess-value end with file-ending?

I have created an htaccess-file to hide file-endings and prettify URLs. Now trying out everything I witness echoing a value from a prettified URL shows the file-ending in the result.
This varied a lot as I wasn't sure where the problem is. So there was ([0-9a-zA-Z]+) for example. I have also tried solutions from StackOverflow.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^events/([^/]+)$ viewevent.php?c=$1 [L,QSA]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
And my viewevent.php-file
<?
echo $_GET["c"];
?>
I used to try different variations, but it's either error or with the file-ending. And the URL I enter is: url/events/bino.
I expect bino, but I get bino.php.
Where is the problem?
Your rules need more reordering otherwise in present state it will redirect http://example.com/events/foo to https://example.com/viewevent.php?c=foo thus exposing your internal handler.
Moreover you need to add .php for files that actually exist in your filesystem as php file.
You may use this code:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^events/([^/]+)/?$ viewevent.php?c=$1 [L,NC,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
The answer was simple. Put RewriteRule ^([^\.]+)$ $1.php [NC,L] at the end of the file, else it's kind of mixing with the other rules.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^events/([^/]+)$ viewevent.php?c=$1 [L,QSA]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteRule ^([^\.]+)$ $1.php [NC,L]

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]

htaccess rewriting if statements

I am working on a twitter/facebook type site for a college class. Somehow they let a professor teach this class with no PHP, CSS, HTML, JavaScript,jQuery, or Ajax knowledge. I have been trying to rewrite my URLs to make them look like twitter. I have gotten all of my user profile pages to rewrite to: www.site.com/username from: www.site.com/profile.php?name=username. However, I also want to rewrite my login page, create account page, etc. Currently they are: www.site.com/login.html , www.site.com/createAccount.html. I want the to rewrite without the html. Here is my .htaccess file currently.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
RewriteRule ^([a-zA-Z0-9_-]+)$ profile.php?name=$1 [L]
**Update
I apologize I ended up switching all of my files over to .php. But I am still having some issues. Now, I only want to redirect specific URLs. For example: www.361orc.info/login should internally redirect to www.361.orc.info/login.php . I cannot seem to figure out what is wrong with the following code. It redirects but it does it changes the client URL. I want it to just redirect internally. Here is my .htaccess file:
RewriteEngine On
#I want this code to change .com/login.php to .com/login but only internally
#the URL in the client's browser shouldn't change
RewriteCond %{REQUEST_FILENAME} !-
RewriteRule ^login?$ login.php [L]
#Change the profile pages of users from .com/profile.php?name=user to .com/user
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([\w-]+)/?$ /profile.php?name=$1 [L,QSA]
you could do something like this
# Rewrite User Profiles
RewriteCond %{QUERY_STRING} ^name=([^&]+) [NC]
RewriteRule ^profile.php$ /%1 [R=301,L]
# Rewrite Login
RewriteRule ^login.html$ /login [R=301,L]
# Rewrite Create Account
RewriteRule ^createAccount.html$ /createAccount [R=301,L]
You are pretty close, just an external redirection rule that will redirect .html files to ones without .html extension:
RewriteEngine On
RewriteBase /
# To externally redirect /dir/file.html to /dir/file
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(.+?)\.html[\s?] [NC]
RewriteRule ^ %1 [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^([^\.]+?)/?$ $1.html [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([\w-]+)$ profile.php?name=$1 [L,QSA]

How to replace "-" with "_" and remove index.php from the address bar

I have developed my website in codeigniter. I have a couple of things to accomplish in .htaccess file.
For any page/resource other than index.php, img, js, css i want it to add index.php before the actual resource in the url.
I have so many static pages having unserscore "_" in them, e.g, contact_us.php, our_services.php etc. I want when user gives the url like www.mywebsite.com/our-services it should open up the original page, that is, www.mywebsite.com/our_services. There are 1,2 upto 7 underscores for different pages, e.g, mywebsite.com/speech_writing_services
Here is my .htaccess file which i could build up so far:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^(/index\.php|/img|/js|/StyleAdmin|/images|/StyleAdmin/images|/css|/robots\.txt|/favicon\.ico)
RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteRule ^([^-]*)-([^-]*)$ http://www.mywebsite.com/$1_$2 [R=301,L]
RewriteRule ^([^-]*)-([^-]*)-([^-]*)$ http://www.mywebsite.com/$1_$2_$3 [R=301,L]
RewriteRule ^([^-]*)-([^-]*)-([^-]*)-([^-]*)$ http://www.mywebsite.com/$1_$2_$3_$4 [R=301,L]
RewriteRule ^([^-]*)-([^-]*)-([^-]*)-([^-]*)-([^-]*)$ http://www.mywebsite.com/$1_$2_$3_$4_$5 [R=301,L]
RewriteRule ^([^-]*)-([^-]*)-([^-]*)-([^-]*)-([^-]*)-([^-]*)$ http://www.mywebsite.com/$1_$2_$3_$4_$5_$6 [R=301,L]
The problem here is that when i give the url like www.mywebsite.com/contact_us it works just fine and shows the same url in the address bar. But when i give this url www.mywebsite.com/contact-us it does show the page but the url shown in the address bar of the browser becomes www.mywebsite.com/index.php/contact_us whereas i want it to be like www.mywebsite.com/contact_us with index.php removed.
You need to do the routing after the redirecting.
RewriteEngine on
RewriteRule ^([^-]*)-([^-]*)$ http://www.mywebsite.com/$1_$2 [R=301,L]
RewriteRule ^([^-]*)-([^-]*)-([^-]*)$ http://www.mywebsite.com/$1_$2_$3 [R=301,L]
RewriteRule ^([^-]*)-([^-]*)-([^-]*)-([^-]*)$ http://www.mywebsite.com/$1_$2_$3_$4 [R=301,L]
RewriteRule ^([^-]*)-([^-]*)-([^-]*)-([^-]*)-([^-]*)$ http://www.mywebsite.com/$1_$2_$3_$4_$5 [R=301,L]
RewriteRule ^([^-]*)-([^-]*)-([^-]*)-([^-]*)-([^-]*)-([^-]*)$ http://www.mywebsite.com/$1_$2_$3_$4_$5_$6 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^(/index\.php|/img|/js|/StyleAdmin|/images|/StyleAdmin/images|/css|/robots\.txt|/favicon\.ico)
RewriteRule ^(.*)$ /index.php/$1 [L]

Resources