htaccess: strip .html extension if present then rewrite - .htaccess

My htaccess is getting stuck in an infinite loop. I'm trying to strip the .html extension off a URL, if there is one, and then use the leftover string as a query string.
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.html\ HTTP/
RewriteRule ^(.*)\.html$ /$1 [R=302,L]
RewriteRule ^resources/blog$ /resources/blog/ [R=302,L,QSA]
RewriteRule ^resources/blog/$ resources/blog.php [L,QSA]
RewriteRule ^resources/blog/.*?([^\.\/]*)$ resources/blog.php?pname=$1 [L,QSA]
UPDATE:
Here's how it sit at this time. I've commented some stuff out while troubleshooting.
Options +SymLinksIfOwnerMatch -MultiViews
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#Redirect to URL without .html extension (Pretty URL)
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.html\ HTTP/
RewriteRule ^(.*)\.html$ /$1 [R=302]
# Pretty url for Couch CMS blog
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^resources/blog/.*?([^\.\/]*)$ resources/blog.php?pname=$1 [L,QSA,END]
RewriteRule ^resources/blog$ /resources/blog/ [R=302,L,QSA]
RewriteRule ^resources/blog/$ resources/blog.php [L,QSA]
# RewriteRule ^resources/blog/([1-2]\d{3})/(?:(0[1-9]|1[0-2])/(?:(0[1-9]|1[0-9]|2[0-9]|3[0-1])/)?)?$ resources/blog.php?d=$1$2$3 [L,QSA]
# RewriteRule ^resources/blog/[^\.]*?([^/\.]*)/$ resources/blog.php?fname=$1 [L,QSA]
# RewriteRule ^resources/blog/[^\.]*?([^/\.]*)$ "$0/" [R=302,L,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)$ $1.html [L,QSA]
</IfModule>
UPDATE 2
File structure is as follow:
/
|-- static_html_files/
|-- resources/
|----- blog.php
|----- newsletter.html
|-- .htaccess
|-- index.html
Most files are static and their URL's need to be 'prettified' by stripping the .html extension from them and then redirected to with the pretty URL.
The blog.php has to be prettified as well and the Couch CMS generated links need to be rewritten. The rule for rewriting the couch cms links is the one that keeps looping.
UPDATE 3
It seems there is a 301 redirect somewhere but I don't have a 301 in my htaccess? Not sure where to go from here or where to look for this redirect. See Redirect Check results below:
https://xxxxx.ca/resources/blog/yyyyy
HTTP/1.1 301 Moved Permanently
Date: Tue, 25 Jun 2019 20:58:31 GMT
Server: Apache
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate
Pragma: no-cache
Set-Cookie: PHPSESSID=poplcjkqcgd4gk57c1l3lcdjg7; path=/
Location: https://xxxxx.ca/resources/blog/yyyyy.html
Transfer-Encoding: chunked
Content-Type: text/html; charset=UTF-8
https://xxxxx.ca/resources/blog/yyyyy.html
HTTP/1.1 302 Found
Date: Tue, 25 Jun 2019 20:58:31 GMT
Server: Apache
Location: https://xxxxx.ca/resources/blog/yyyyy
Content-Length: 263
Content-Type: text/html; charset=iso-8859-1
[RINSE & REPEAT AD NAUSEAM]
UPDATE 4
After some debugging, I seem to be having an Apache problem with mod_rewrite and 'add path info postfix' which is generally solved by specifying -MultiViews and/or adding the DPI flag to the rules but neither of these seem to solve any of my problems.

Related

Excluding Maintenance Page in specific directory

Currently I use this code in my .htaccess file to trigger my Site's maintenance page.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/maintenance.html -f
RewriteCond %{DOCUMENT_ROOT}/maintenance.enable -f
RewriteCond %{SCRIPT_FILENAME} !maintenance.html
RewriteRule ^.*$ /maintenance.html [R=503,L]
ErrorDocument 503 /maintenance.html
Header Set Cache-Control "max-age=0, no-store"
</IfModule>
How Would I make it so I could still access a directory I would be working On and not just my IP address in case my friend wishes to access it.
I have done some searching, but not found anything as of yet
Cheers
Tom
I'd say you can simply implement some exception rules prior to your handling of the maintenance mode:
<IfModule mod_rewrite.c>
RewriteEngine On
# immediately end all rewriting for specific IPV4 addresses
RewriteCond %{REMOTE_ADDR} ^123\.123\.123\.123$ [OR]
RewriteCond %{REMOTE_ADDR} ^321\.321\.321\.321$
RewriteRule ^ - [END]
# for everyone else: check for maintenance mode
RewriteCond %{DOCUMENT_ROOT}/maintenance.html -f
RewriteCond %{DOCUMENT_ROOT}/maintenance.enable -f
RewriteCond %{SCRIPT_FILENAME} !maintenance.html
RewriteRule ^.*$ /maintenance.html [R=503,L]
ErrorDocument 503 /maintenance.html
Header Set Cache-Control "max-age=0, no-store"
</IfModule>
Alternatively you could add the negated conditions as additional conditions to the maintenance mode rewriting logic. But I think the above is easier to read and maintain.

codeigniter redirects and search engine bots

I am new to codeigniter. In fact, I'm not even working on it. I'm just employed to work the SEO for a website. I only have access to the cpanel of the site, not the dashboard of the codeigniter (I wonder if there is one?)
So far I have been able to figure out everything that needs my attention. Only problem now is the search bots seems to be hitting redirects. When I tried to fetch as Bingbot, I get the following
HTTP/1.1 301 Moved Permanently
Connection: close
Date: Sun, 12 Jun 2016 00:34:29 GMT
Content-Length: 234
Content-Type: text/html; charset=iso-8859-1
Location: http://www.domain.com/
Server: Apache
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>301 Moved Permanently</title>
</head><body>
<h1>Moved Permanently</h1>
<p>The document has moved here.</p>
</body></html>
I figured it might be the redirect rules written into the .htaccess. But I really don't know what to change to allow for the bot to smoothly crawl the site. View .htaccess below.
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
What do I need to add to the .htaccess file to allow search engines crawl the site without hitting to http 301 again? Thanks
Why did you put this code in .htaccess file?
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]

How to upload laravel 5 project in Linux hosting server?

I am trying to upload laravel 5 project to a live hosted Linux server. When I browse to a url it shows many errors.
Error- 404 Page Not Found
and
Forbidden You don't have permission to access / on this server.
When I check my Error log file
[Mon Dec 28 12:10:57 2015] [error] [client 60.243.107.196] Directory index forbidden by Options directive: /home/riseyourmoney/public_html/vdl/
So I changed my .htaccess file
<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>
There is any routing problem?

Htaccess rule giving 301 error

I am trying to change a URL, in my htaccess I have added this line:
RewriteRule ^/about/$ /site/about_us/
After adding this, if I hit my URL http://mydomain.com/about/ it gives me 301 moved permanently and redirects to http://mydomain.com/site/about_us/
My htaccess file:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteRule ^about/?$ /site/about_us/ [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Headers:
Response Heades:
HTTP/1.1 301 Moved Permanently
Date: Wed, 18 Jun 2014 09:42:45 GMT
Server: Apache/2.4.4 (Win64) PHP/5.4.12
X-Powered-By: PHP/5.4.12
X-Pingback: http://soller.local/xmlrpc.php
Expires: Wed, 11 Jan 1984 05:00:00 GMT
Cache-Control: no-cache, must-revalidate, max-age=0
Pragma: no-cache
Location: http://soller.local/site/about_us/
Content-Length: 0
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: text/html; charset=UTF-8
Request Heades:
GET /about/ HTTP/1.1
Host: soller.local
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:29.0) Gecko/20100101 Firefox/29.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Cookie: wp-settings-time-1=1393072512; wp-settings-1=libraryContent%3Dupload%26editor%3Dtinymce%26hidetb%3D1; wp-settings-time-47=1390228546; wp-settings-time-877=1392096718; wp-settings-time-2579=1393680813; wp-settings-time-2578=1393427211; wp-settings-time-106=1394689670; wp-settings-time-4965=1396012806; PHPSESSID=28tioc16rbc3314br3smf9djv7; wordpress_logged_in_3bbc4cdbbb4e6dcd108616247c041571=nirajmchauhan%40gmail.com%7C1403246168%7C08fbbba723e8d02b102a50fb5d0dee38; last_navigator_url=http%3A%2F%2Fsoller.local%2Fsite%2Fprofile%2Fmy-assessments%2F; url_route_slug=%2Fniraj; current_navigator_url=http%3A%2F%2Fsoller.local%2Fniraj
Connection: keep-alive
You should remove the [PT] rewrite flag.
You can learn more about rewrite flags here: http://httpd.apache.org/docs/current/rewrite/flags.html#flag_pt
ADDITION
Also you might need to move your rewriterule before the wordpress rules. your code will look like this
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteRule ^about/?$ /site/about_us/ [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

.htaccess: Image Redirect

I was shown this rewrite for getting images to redirect to a particular web page. And it allows for the usage of images on my site and others.
RewriteCond %{HTTP_REFERER} ^$
RewriteCond %{REQUEST_URI} ^/uploads/(\d+)-(.+)\.(png|gif|jpe?g|bmp)$ [NC]
RewriteRule (.*) http://www.sitename.com/view/%1-%2/
This works great if I try to directly access an image from the address bar and I'm currently viewing my site. The problem I am noticing is if a user writes out a link on another site:
Click
It will take them to that image, not to the page.
What modifications should I be making to the rewrite so that it will redirect the user to the /view/ page.
EDIT: Another thing I have noticed is when posting an image to Facebook, using the above rewrite, it will not display the thumbnail as it once did. I am assuming this is something that cannot be worked around?
RewriteCond %{HTTP_REFERER} ^$ [OR]
RewriteCond %{HTTP_REFERER} !^http://domain.com
RewriteCond %{REQUEST_URI} ^/uploads/(\d+)-(.+)\.(png|gif|jpe?g|bmp)$ [NC]
RewriteRule (.*) http://domain.com/view/%1-%2/
For prevent caching:
<filesMatch "\.(png|jpeg|jpg|gif)$">
Header unset ETag
Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
Header set Pragma "no-cache"
Header set Expires "Wed, 11 Jan 1984 05:00:00 GMT"
</filesMatch>

Resources