Alright so this is what the root of my site looks like:
assets
css
js
img
.htaccess
index.php
page1.php
page2.php
page3.php
style.css
My current .htaccess file works in the sense that it hides .php from the pages which is what i want. So i can access it from http://example.com/page2. But the problem is if i go to http://example.com/page2/ You can see the raw code without any CSS. I want to either redirect users to somewhere else OR have it show it correctly regardless of if there is a "/" or not.
Making it a directory is not an option. It has to be a PHP file in the root.
Current .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
Change your css links to absolute URLs (the start with a /) or add this to the header of your pages:
<base href="/">
Related
I have added .php extension to all my pages in wordpress through functions.php.
My Code -
<?php
// Do NOT include the opening PHP tag
// Add .PHP to page permalinks
add_action('init', 'ss_php_pages', -1);
function ss_php_pages() {
global $wp_rewrite;
if ( !strpos($wp_rewrite->get_page_permastruct(), '.php')){
$wp_rewrite->page_structure = $wp_rewrite->page_structure . '.php';
}
$wp_rewrite->flush_rules();
}
// Remove slash from page permalinks
add_filter('user_trailingslashit', 'no_page_slash_on_ss',66,2);
function no_page_slash_on_ss($string, $type){
global $wp_rewrite;
if ($wp_rewrite->using_permalinks() && $wp_rewrite->use_trailing_slashes==true && $type == 'page'){
return untrailingslashit($string);
}else{
return $string;
}
}
Now I want to redirect my old link (example.com/demo) to new link (example.com/demo.php)..
I have many pages there so I can't add redirection for each manually.
Additional information:
I have added .php extension to my Wordpress pages (example.com/demo + .php). Now My Old Links (example.demo or example.com/anything) which were without .php, got 404 because I have added .php to them so they have modified. Now I want to redirect my all old links like example.com/demo to example.com/demo.php or example.com/xyz to example.com/xyz.php to not to loss traffic or getting broken link.
You can redirect your non-php pages to php using RewriteRule directive in your WordPress htaccess.
At the top of your htaccess file , put the following rule :
RewriteEngine on
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ /$1.php [L,R=301]
Could you please try following htaccess Rules. Make sure htaccess is present in root folder.
Make sure to clear your browser cache before testing your URLs.
RewriteEngine on
##User is using wordpress without this rule it was adding .php to home page too which was NOT needed, hence this rule is been added.
RewriteRule ^/?$ / [R=301,END]
##Major rules for links which are non php and should be served with php format files.
RewriteCond %{DOCUMENT_ROOT}/$1.php -f [NC]
RewriteCond %{REQUEST_URI} !\.php/?$ [NC]
RewriteRule ^(.*)/?$ $1.php [L,R=301]
For JS/CSS rewrite: You may need to use base tag to fix your js and other relative resources. If you are linking js files using a relative path then the file will obviously get a 404 because its looking for URL path. for example if the URL path is /file/ instead of file.html then your relative resources are loading from /file/ which is not a directory but rewritten html file. To fix this make your links absolute or use base tag. In the header of your webpage add this <base href="/"> so that your relative links can load from the correct location.
My website is pretty standard containing a bunch of index files inside folders. ie:
folder1
> index.php
> some-file.php
sub-folder1
> index.php
sub-folder2
> index.php
folder2
> some-file.html
index.html
I recently made some .htaccess changes that were supposed to allow a user to enter a file name without the .html extension but, still be directed to the correct file on the server. It was also supposed to remove trailing slashes. This is the code in my root .htaccess file:
DirectoryIndex index.html
RewriteEngine On
# remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} \s(.+?)/+[?\s]
#RewriteRule ^(.+?)/$ /$1 [R,L]
RewriteRule ^(.+?)/$ /$1 [R=301,L]
# To internally forward /dir/file to /dir/file.html
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.html -f [NC]
RewriteRule ^(.+?)/?$ /$1.html [L]
This seems to work fine with html files. such as including a link to:
folder2/some-file
However, when I link to folder1/ where the index file is a php file, I'm directed to a list of files and folders inside folder1. I would expect the behavior to pull up index.php not the file list.
If I include the entire path in a link such as: folder1/index.php it works just fine.
This behavior all changed when I added the .htaccess setting above. Before this I didn't even have a .htaccess file on my site. I'm assuming it had to do with the code inside the .htaccess file but, I have no idea how to fix it, as the code it something I found on a help forum.
I also noticed that before I changed my .htaccess setting I could link to my root index.html from any page and the browser would just display www.domain.com. now it always shows www.domain.com/index
Wondering if anyone knows the correct setting I should be using inside my .htaccess file?
You made DirectoryIndex index.html and if there is no index.html in directory ,listing will be done even there is index.php there .
Add it like this :
DirectoryIndex index.html index.php
So , if there is no index.html request to directory will go to index.php .
I am having another issue with .htaccess where I can't seem to find the right solution for it. What I am trying to accomplish is to redirect from a subdirectory to a specific html document within that same directory when you copy/paste the url displayed below
For example:
http://example.com/staging/test/ to http://example.com/staging/test/page.html
My (almost) working method:
Options -Indexes -MultiViews
RewriteEngine on
RewriteRule ^test/(.*)$ test/page.html [L]
The problem with this code is that it does basically redirect to the correct page.html but if you want to click on any link on page.html it just redirects to page.html again instead of going to a particular link within subdirectory /test/ let's say /test/page-2.html
Important: This redirect should only affect the subdirectory "staging". All other sub-directories including root can not be affected.
Other info:
This code is also in my .htaccess file to remove the .html extension for the directory "staging"
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ $1.html [L]
The directory "staging" is not connected to any CMS and only consists of .html files.
I am on a shared hosting environment (Hostgator/cPanel).
Thank you.
Use below rule, you are using only L flag but I am modifying it for redirect as you mentioned.
Options -Indexes -MultiViews
RewriteEngine on
RewriteRule ^test/$ /staging/test/page.html [R=301,L]
You want to redirect only the directoy, but not the files in it. The pattern in the rule says match the directory test/ followed by anything .*, including any files.
To match the directory alone, the pattern must stop at test, which is accomplished by an end of string anchor
RewriteRule ^test/$ /staging/test/page.html [R,L]
Instead of an absolute path, you can keep the relative path and add a RewriteBase directive
RewriteBase /staging
RewriteRule ^test/$ test/page.html [R,L]
If you want to rewrite only, leave out the R flag.
i use the following .htaccess to redirect all requests to my index.php except if a folder or file exists.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?uri=$1 [QSA]
But if i try url's like domain.com/thing/a/thing/b i get redirected to my index.php but everything else is messed up. The html doesn't link properly to the stylesheet and so on.
My application is working kinda like a MVC-Framework, i.e. everything happens over the index.php. So i don't want that other files are accessible except files in a public folder and some specific files. Namely cron.php in the root and some css/js/image files down the folder structure.
More details on the file structure: https://bitbucket.org/BrainInBlack/source-motd-plus/src
Or do i have to link stylesheets and other stuff with absolute paths, i.e. "http://domain.com/path/to/style.css" and so on?
use a slash client before (.*)$ index.php?uri=$1.
use this code:
RewriteRule ^/client(.*)$ index.php?uri=$1
You either need to change all your links to absolute links or add a
<base href="http://domain.com/">
to the header of your pages.
I have my directories setup like so:
mywebsite/directory1/index.php
mywebsite/directory2/index.php
mywebsite/directory3/index.php
mywebsite/directory4/index.php
this is not SEO friendly. How can I use mod_rewrite to do the following:
mywebsite/directory1/
mywebsite/directory2/
mywebsite/directory3/
How can I make search engines crawl my site without the issues of having multiple index.php´s?
What are you professional suggestions?
Place this into your root .htaccess file:
RewriteEngine On
#if your index.php is located in the root folder:
RewriteBase /
#or use that, if the path for your index.php is '/some/subfolder/index.php':
#RewriteBase /some/subfolder/
#rewriting only if the request file doesn't exists (you don't want to rewrite request for images or other existing files)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)? index.php?path=$1 [L]
After that, you can use the REQUEST_URI in your root index.php file:
var_dump($_SERVER['REQUEST_URI']);
or you can use $_GET['path']:
var_dump($_GET['path']);
In your HTML and CSS, use absolute path for images and other resources (or better use full URL's):
<img src="/image/image.jpg" alt="image" />