how to redirect non php extension url to php extension url htaccess? - apache

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.

Related

Why can't I access my page and query params after htaccess rules rewrite the url?

I have a url of the form --> https://websitename.com/content?id=xyz
I want to rewrite the url in the form --> https://websitename.com/content/xyz
I want to be able to access the query param (id) and its value (xyz) on the content page after the url has been rewritten
All pages are *.php pages
I've used the following htaccess rewrite rule
RewriteEngine On
# Hide .php file extension in the urls
RewriteRule ^([^/.]+)$ $1.php
RewriteRule ^content/([A-Za-z0-9-]+)/?$ content?id=$1 [NC,L]
When I navigate to the rewritten url, it doesn't work and I get the following error:
"Not Found
The requested URL was not found on this server"
I also can't access the query parameter and its value on the rewritten url
But, when I enter the original url, that works and I can access the query parameter and its value
I'd appreciate it a ton if someone can help me find out what I am doing wrong and how I can fix this.
Thanks,
Dexxterr.
With your shown samples, please try following once. Please clear browser cache before testing your URLs.
RewriteEngine On
# Hide .php file extension in the urls
RewriteRule ^([^/.]+)$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^content/([A-Za-z0-9-]+)/?$ content.php?id=$1 [NC,L]
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.

Display content based on URL

In my site I had a desire to make simpler URLs such as:
http://www.example.com/about
Instead of:
http://www.example.com/about.php
Mostly for aesthetics, for now I have Apache redirect to ./default.php if a directory is requested. However this forces me to create a directory at
http://www.example.com/about/ and a file inside it called default.php ending up with:
http://www.example.com/about/default.php
I know there's a better way, probably using PHP or JS, what do?
it's all in apache's configs,
rewriteEngine can do that for you
http://httpd.apache.org/docs/current/mod/mod_rewrite.html
http://httpd.apache.org/docs/2.0/misc/rewriteguide.html
http://alexcican.com/post/how-to-remove-php-html-htm-extensions-with-htaccess/
A snippet taken from third link:
To remove the .php extension from a PHP file for example yoursite.com/wallpaper.php to yoursite.com/wallpaper you have to add the following code to your Apache config:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
If you want to remove the .html extension:
RewriteRule ^([^\.]+)$ $1.html [NC,L]
That’s it! You can even link pages inside the HTML document without needing to add the extension of the page. For example:
wallpaper

rewriterule image to php script and joomla not working

I want to e able to serve some image whose content is based on a payment condition.
The image should be available on http://example.com/banner/image_**code**.gif
(code is different for every client.)
rewriterule should change that link to index.php?option=com_joomlacomponent&img=**code**
I wrote what I supposed to be a very simple rule to accomplish that but it doesn't work.
I added the rewriterule to an existing .htaccess in the document root.
The .htaccess contains rules to add the standard slash, www, to redirect old php files to new php files and to add to the path the name of the directory where there's the joomla install.
The structure of the file/folders is as follows:
/.htaccess
/cms/.htaccess (joomla standard .htaccess)
/cms/* (joomla files)
I tried the following rules:
RewriteRule ^banner/image_(.*)\.gif$ index.php?option=com_joomlacompanent&
img=$1
I get the homepage of joomla (adding various flags didn't help, e.g. [L].)
I tried adding a rewritecond to check if the condition was satisfied:
RewriteCond %{REQUEST_URI} /banners/image_(.*).gif$
RewriteRule . http://othersite.com/%1 [L]
and I get redirected to othersite/code where code is the code in the initial url.
So I wonder if I'm missing some important concept of rewriterule or if there's some interference between this rule and other rules or joomla .htaccess.
EDIT: I found out what the problem is.
I wrongly supposed that my php could temporarily serve a debug message (an echo) instead of a real image.
Probably joomla doesn't allow that redirecting to the homepage.
Now I'm using the following rule in the .htaccess in document root:
RewriteCond %{REQUEST_URI} /banner/image_(.*).gif$
RewriteRule .* index.php?option=com_joomlacomponent&img=%1
And in the component php file:
$img = 'path to an img file';
header('Content-Type: image/gif');
readfile($img);
$mainframe =& JFactory::getApplication();
$mainframe->close();
exit;
I think the question can be flagged as Solved.
Since your rules are in the htaccess file in your document root, don't you need to call into the cms folder?
RewriteRule ^banner/image_(.*)\.gif$ /cms/index.php?option=com_joomlacompanent&img=$1 [L]
Also note that you'll want that rule relatively close to the top (but below any rules that redirect).

PHP Files/Directories - .htaccess

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="/">

Change site URL in Apache with mod_rewrite

How can i change my site URL using mod_rewrite my problem is when i open my site's admin section i have to write www.example.com/index.php/admin
what i want is i directly open my site like www.example.com/admin please help
thanks
Here's a .htaccess file copied from the CodeIgniter PHP framework:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
This redirects everything that isn't index.php, robots.txt or anything in the images folder to index.php/$1, where $1 is the URL the user entered.
You need these rules as:
Redirecting index.php would cause an infinte loop - http://localhost/index.php/index.php/index.php/...
robots.txt is an important file that search engines use. It is just plain text; you don't want it handled by your code.
You obviously want to keep having access to your images. Adjust this path as necessary for where your static assets are located.
Try this rule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !^index\.php/ index.php%{REQUEST_URI} [L]
The condition is to exclude requests of existing files to be rewritten.