Sorry if I explain my problem badly, It's a little hard to explain what's going wrong*
I've been playing around with the .htaccess file for a while now, Trying to get this to work.
I have the following files in my website directory:
- index.php
- generator.php
- menu.php
- gift-history.php
- stats.php
- images/
- bg-stars.png
- avatar.png
- css/
- style.css
- glow.css
- background.css
- js/
- script.js
- jquery.js
- destiny.js
- snowflake.js
The three folders are images,css and js.
The generator.php requires a $_GET paremeter to know what generator to load.
So far, i've been using the following url:
http://tryhardhusky.com/generator.php?t=destiny
For the other pages, I have the following code in my .htaccess file:
php_value display_errors Off
php_flag magic_quotes 1
php_flag magic_quotes_gpc 1
php_value mbstring.http_input auto
php_value date.timezone America/New_York
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
## hide .php extension snippet
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
This pretty much removes the .php extension from the file name, So files that would be:
http://tryhardhusky.com/gift-history.php
becomes:
http://tryhardhusky.com/gift-history
I'm trying to get the following page:
http://tryhardhusky.com/generator.php?t=destiny
to rewrite as
http://tryhardhusky.com/generator/destiny
All my previous attempts have broken the js and css, Returning a 404 error when loading the page (despite the location being correct)
[Error 404 Not Found: http://tryhardhusky.com/js/jquery.js ]
Any clue how I can do this without breaking it?
You can replace your current code by this one in your htaccess
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
# rewrite /generator.php?t=XXX to /generator/XXX
RewriteCond %{THE_REQUEST} \s/generator\.php\?t=([^\s&]+)\s [NC]
RewriteRule ^ generator/%1? [R=301,L]
# hide PHP extension for existing files
RewriteCond %{THE_REQUEST} \s/(.+?)\.php(?:\s|\?) [NC]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ %1? [R=301,L]
# internally rewrite /generator/XXX to /generator.php?t=XXX
RewriteRule ^generator/([^/]+)$ generator.php?t=$1 [L]
# internally rewrite to PHP file extension (if existing)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.+)$ $1.php [L]
Related
My shop is supposed to work on monday ... and it is not ready (yet). Stress ...
Recently I moved to SSL and remove the now useless domain mapping plugin.
I am experiencing various problems ( a) download of downloadable files on my WooCommerce shop often fail , b) the theme displayed on my main site homepage is not the one activated ...)
Debug mode is on but no error appears in my pages.
My logs are full of errors :
AH10039: FastCGI: server
and
server and Request exceeded the limit of 10 internal redirects due to probable configuration error.
I suspect my htaccess to be causing at least the 2nd error. ( And I am pretty ignorant in that matter ...) Here it is :
#WFIPBLOCKS - Do not remove this line. Disable Web Caching in Wordfence to remove this data.
Order Deny,Allow
#Do not remove this line. Disable Web Caching in Wordfence to remove this data - WFIPBLOCKS
#SetEnv PHP_VER 5
#SetEnv REGISTER_GLOBALS 0
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
# add a trailing slash to /wp-admin
RewriteRule ^wp-admin$ wp-admin/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^(wp-(content|admin|includes).*) $1 [L]
RewriteRule ^(.*\.php)$ $1 [L]
RewriteRule . index.php [L]
</IfModule>
# END WordPress
# ultimate hotlink protection
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} \.(gif|jpe?g?|png)$ [NC]
RewriteCond %{HTTP_REFERER} !^https?://([^.]+\.)?mainDomain\. [NC]
RewriteCond %{HTTP_REFERER} !^https?://([^.]+\.)?subDomain1\. [NC]
RewriteCond %{HTTP_REFERER} !^https?://([^.]+\.)?subDomain2\. [NC]
RewriteCond %{HTTP_REFERER} !^https?://([^.]+\.)?subDomain3\. [NC]
RewriteCond %{HTTP_REFERER} !^https?://([^.]+\.)?subDomain4\. [NC]
RewriteRule \.(gif|jpe?g?|png)$ - [F,NC,L]
</ifModule>
<FilesMatch "\.(ttf|otf|woff)$">
<IfModule mod_headers.c>
Header set Access-Control-Allow-Origin "*"
</IfModule>
</FilesMatch>
Thanks for any advice !
Nicolas
Solved !
I simply went back to the standard .htaccess given in the network settings
We are getting a lot of 404 errors on a Real Estate website after houses are sold and the listing goes offline. I am trying, via htaccess, to redirect the missing pages that Google Search Console shows as 404 to a home listing search page. I have tried the code below but it is redirecting all listing pages not just the ones that no longer exist. Not sure if it's my code or because the pages are dynamically created.
All the home listings are under www.example.com/homes-for-sale-details/[address]. If the listing no longer exists I want the page to redirect to www.example.com/homes-for-sale-details.
My htaccess code
# Redirect old home listing to a search page
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^homes-for-sale-details/(.*)$ https://www.example.com/homes-for-sale-details [L,NC,R=301]
</IfModule>
Where are I going wrong?
Many Thanks!
Edit (added more of htaccess code):
####################################
# START Redirect pages from old site
#
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^Properties https://www.example.com/home-listings [L,NC,NE,R=301]
RewriteRule ^Access https://www.example.com/search-homes [L,NC,NE,R=301]
RewriteRule ^Neighboorhoods https://www.example.com/neighborhoods [L,NC,NE,R=301]
RewriteRule ^Buyer-Resources https://www.example.com/buy-home-in-colorado-springs [L,NC,NE,R=301]
RewriteRule ^Relocation-Guide https://www.example.com/buy-home-in-colorado-springs/relocation-guide [L,NC,NE,R=301]
RewriteRule ^Buyer-Resources/Buyer-Finance/Finance-Information https://www.example.com/buy-home-in-colorado-springs/home-finance [L,NC,NE,R=301]
RewriteRule ^Seller-Resources https://www.example.com/sell-colorado-springs-home [L,NC,NE,R=301]
RewriteRule ^Area-Schools https://www.example.com/local-lifestyle/area-schools [L,NC,NE,R=301]
RewriteRule ^Colorado-Springs-Attractions https://www.example.com/local-lifestyle/colorado-springs-attractions [L,NC,NE,R=301]
RewriteRule ^Military-Bases https://www.example.com/local-lifestyle/military-bases [L,NC,NE,R=301]
RewriteRule ^About$ https://www.example.com/about-us [L,NC,NE,R=301]
RewriteRule ^contact$ https://www.example.com/contact-us [L,NC,NE,R=301]
RewriteRule ^Terms-Of-Service https://www.example.com/terms-of-service [L,NE,R=301]
RewriteRule ^Privacy-Policy https://www.example.com/privacy-policy [L,NE,R=301]
RewriteRule ^Site-Map https://www.example.com/sitemap [L,NC,NE,R=301]
RewriteRule ^neighborhoods/fountain$ https://www.example.com/neighborhoods/fountain-security-widefield [L,NC,NE,R=301]
RewriteRule ^neighborhoods/securitywidefield https://www.example.com/neighborhoods/fountain-security-widefield [L,NC,NE,R=301]
RewriteRule ^park-avenue-properties-blog https://www.example.com/blog [L,NC,NE,R=301]
RewriteRule ^Primary-Factors-the-Affect-the-Real-Estate-Market https://www.example.com/primary-factors-affect-real-estate-market [L,NC,NE,R=301]
</IfModule>
# END Redirect pages from old site
# Force HTTPS
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/\.well-known/acme-challenge/[0-9a-zA-Z_-]+$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
</IfModule>
# Remove "Blog" from blog post URLs and preserve blog paging
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !page
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/\.well-known/acme-challenge/[0-9a-zA-Z_-]+$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^Blog/(.*)$ /$1 [L,NC,R=301]
</IfModule>
# Redirect old home listing to a search page
#<IfModule mod_rewrite.c>
# RewriteEngine On
# RewriteBase /
# RewriteCond %{REQUEST_FILENAME} !-d
# RewriteCond %{REQUEST_FILENAME} !-f
# RewriteRule ^homes-for-sale-details/(.*)$ https://www.example.com/homes-for-sale-details [L,NC,R=301]
#</IfModule>
####################################
# Browser caching code removed :)
####################################
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
I've tried to get this to work with the .htaccess file and could not. The issue with your script above is that you're not checking first for the 404 error, so all requests from that "folder" are being redirected. I've tried using the ErrorDocument directive in the .htaccess file with no luck, so the solution I worked out was to just use what wordpress already provides. In your themes subfolder (/wp-content/themes/THEMENAME) there should be a 404.php file. If it's not there you can create it. You can use some code at the top of this file to parse the request url and redirect the page. Something like this should work:
<?php
if (strpos($_SERVER['REQUEST_URI'], 'homes-for-sale-details') !== false)
header('Location: /homes-for-sale-details', true, 301);
else {
?>
// ... paste the current 404.php content here
<?php } ?>
Just remember to back up your current 404.php page in case you overwrite something you didn't mean to. If you want to try to get this working with the ErrorDocument stuff in htaccess be my guest, but for whatever reason, it wasn't working for me. Here's a post that shows how to do that.
Good luck!
After unsuccessfully trying to find a htaccess answer slim's answer got me thinging and I found this post WordPress Template Redirect (thank you so much for your help slim!!!)
Here is the modification I made to that code.
// Redirect missing home listing to a search page.
function __404_template_redirect()
{
if( is_404() )
{
$req = $_SERVER['REQUEST_URI'];
if ( is_file( $req )) {
return; // don't reduce perf by redirecting files
}
// check if "homes-for-sale-details" is in the URL
if ( strpos($req, 'homes-for-sale-details') == false ) {
return; // only redirect missing homes
}
// pull the parent directory and convert to site url
$parent_url = get_permalink( 1232 );
// redirect to parent directory
wp_redirect( $parent_url, 301 );
exit();
}
}
add_action( 'template_redirect', '__404_template_redirect' );
I have my htaccess file setup, so that the pages remove extensions. Now, I am trying to make the pages that transfer variables, into SEO friendly urls ... so, for example...
http://www.example.com/art-gallery?page=2 ... which is actually "art-gallery.php?page=2", would turn into... http://www.example.com/art-gallery/page/2
Or... http://www.example.com/art-piece?id=3 ...would go to... http://www.example.com/art-piece/id/3
... and so on ...
I have alot in my htaccess file, and am not sure how to do the above (there are plenty of tutorials on going from www.example.com/index.php?page=2 to www.example.com/page/2/ but none that do exactly what I need). Ideally, I'd like to be able to do this for all similar pages...
# enable the rewrite engine
RewriteEngine On
# Set your root directory
RewriteBase /
# Force www:
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301,NC]
# Remove the .php extension
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
RewriteRule (.*)\.php$ $1 [R=301]
# Remove index and reference the directory
RewriteRule (.*)/index$ $1/ [R=301]
# Remove trailing slash if not a directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/ $1 [R=301]
# Forward request to html file, **but don't redirect (bot friendly)**
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
# Disable Directory Browsing
Options -Indexes
# Disable Hotlinking of Images
# with forbidden or custom image option
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?example.com [NC]
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?google.com [NC]
RewriteRule \.(jpg|jpeg|png|gif)$ – [NC,F,L]
# Protect htaccess File
<files ~ "^.*\.([Hh][Tt][Aa])">
order allow,deny
deny from all
satisfy all
</files>
You can transfer parameters with the variable QUERY_STRING.
Consider the following rule:
RewriteRule ^index.html index.php?%{QUERY_STRING}&m=main&a=index
This rule would transform
index.html?something=value
into
index.php?something=value&m=main&a=index
You should use the RewriteEngine.
You could also use a 301 redirect either alone or in conjunction with the RewriteEngine to redirect SEs.
Generally, though redirecting SEs to a different page than what users will see is not a good practice, and may result in your pagerank decreasing. Instead, try migrating all your pages to the second URL format, and consider using 301 redirects to help the transition.
Generally: Use 301 redirects for SE-friendly page changes. See this SO for additional reference.
You can insert this rule just before Forward request to html file rule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/])/([^/])/([^/])/?$ $1.php?$2=$3 [L,QSA]
This is quite old but why not do the following:
RewriteEngine On
RewriteRule ^([^?]*) index.php?route=$1 [L,QSA]
Then in your index.php you can handle it like such;
if (isset($_GET['route'])) {
$route = explode('/', $_GET['route']);
if (iconv_strlen((end($parts)), 'UTF-8') == 0) {
array_pop($parts);
}
}
From here your main level would be handled with $route[0], second level $route[1]
For example;
http://example.com/art-gallery/2
$route[0] would equal 'art-gallery'
$route[1] would equal '2'
I have the following .htaccess file:
AddDefaultCharset utf-8
RewriteEngine on
Options +SymLinksIfOwnerMatch
RewriteBase /
# redirect all www-requests to no-www
# -
RewriteCond %{HTTP_HOST} ^www\.site\.com$ [NC]
RewriteRule ^(.*)$ http://site.com/$1 [R=301,L]
# redirect all home pages to / (root)
# -
RewriteCond %{THE_REQUEST} ^.*/index\.(php|html?)
RewriteRule ^(.*)index\.(php|html?)$ /$1 [R=301,L]
# remove trailing slash from dirs
# -
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/$ /$1 [R=301,L]
# automatically add index.php when needed
# -
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|login\.php|reg\.php|robots\.txt|css/|js/)
RewriteRule ^(.*)$ /index.php [L]
The .htaccess file should do the following (for SEO):
Conversion to no-www (http://www.site.com should become http://site.com)
All URIs with trailing slashes should convert to no-trailing-slash: http://site.com/me/ should be redirected http://site.com/me
All URIs with index.php/index.html should convert to just nothing: http://site.com/admin/index.php or http://site.com/admin/ should be eventually displayed as http://site.com
However the current version of .htaccess results in a cyclic redirection when trying to access (http://site.com/admin). The real document that should be fetched by browser is http://site.com/admin/index.php.
Can anyone please help me with this issue?
There's a module called mod_dir that's automatically loaded and it causes requests for directories that are missing the trailing slash to get redirected with a trailing slash. You can turn this off using the DirectorySlash directive, but note the security warning when you turn it off. There's an information disclosure security issue if you turn it off and default indexes won't get loaded. However, your lats rule (looks like) it does that, though incorrectly.
First, turn off the DirectorySlash
DirectorySlash Off
Then you need to change the last rule to:
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_FILENAME}/index.php -f
RewriteRule ^(.*)$ /$1/index.php [L]
OBJECTIVE: To cause the browser to rewrite to file-name.php, if it exists; else return file-name.html - whether the visitor has typed the url as any one of the following:
http://mydomain.com/file-name
http://mydomain.com/file-name.html
http://mydomain.com/file-name.php
Had good success with the following rules in my .htaccess file at root:
# REWRITE FILE URI TO file.php IF EXISTS
Options Indexes +FollowSymLinks +MultiViews
Options +ExecCGI
RewriteEngine on
RewriteBase /
# parse out basename, but remember the fact
RewriteRule ^(.*).html$ $1 [C,E=WasHTML:yes]
# rewrite to document.php if exists
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [S=1]
# else reverse the previous basename cutout
RewriteCond %{ENV:WasHTML} ^yes$
RewriteRule ^(.*)$ $1.html
However, I have since installed WP at root, alongside pre-existing website, and these rules are no longer working.
WHAT DOES WORK: file-name is rewritten to either file-name.html or file-name.php - whichever file exists.
WHAT DOES NOT WORK: file-name.html is not rewritten to file-name.php even when there is no file-name.html and file-name.php is there. Also, file-name.php is not rewritten to file-name.html when there is no file-name.php but there is file-name.html.
The entire .htaccess as it is now:
# BEGIN WP MULTISITE RULES
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
# uploaded files
RewriteRule ^([_0-9a-zA-Z-]+/)?files/(.+) wp-includes/ms-files.php?file=$2 [L]
# add a trailing slash to /wp-admin
RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^[_0-9a-zA-Z-]+/(wp-(content|admin|includes).*) $1 [L]
RewriteRule ^[_0-9a-zA-Z-]+/(.*\.php)$ $1 [L]
RewriteRule . index.php [L]
# END WP MULTISITE RULES
# REWRITE FILE URI TO file.php IF EXISTS
Options Indexes +FollowSymLinks +MultiViews
Options +ExecCGI
RewriteEngine on
RewriteBase /
# parse out basename, but remember the fact
RewriteRule ^(.*).html$ $1 [C,E=WasHTML:yes]
# rewrite to document.phtml if exists
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [S=1]
# else reverse the previous basename cutout
RewriteCond %{ENV:WasHTML} ^yes$
RewriteRule ^(.*)$ $1.html
Any advices?
Quick overview tells that your original rules most likely will never get reached as WP rules should intercept all requests.
This line RewriteRule ^ - [L] with those conditions will abort rewriting for any already existing files or folders, while this line RewriteRule . index.php [L] will intercept/redirect all requests to index.php.
If you move your rules above WordPress one, then it will work again.
To rewrite request for non-existing .php file to .html file use this rule:
# rewrite non-existing .php file to existing .html
RewriteCond %{DOCUMENT_ROOT}/$1.php !-f
RewriteCond %{DOCUMENT_ROOT}/$1.html -f
RewriteRule ^(.+)\.php$ $1.html [L,PT]
Place it below your rules (but above WP). The rule will check if .php files does not exist and rewrite will only occurs if .html file is present. If both files are unavailable then nothing will happen.
Keep in mind that because of these checks and the fact that rule is on the top of rewrite chain, this rule will be evaluated for every request to .php file (even WP pages) which may put extra pressure on very busy server. Ideally you would like to have proper URLs in first place so there will be no need for such manipulations.