My website is a custom made PHP website. I recently made it SEO friendly by copying the .htaccess file from wordpress and modify it a little. The problem is that some pages are realoaded twice especialy pages that have more than 1 backslash like download/something/ . I have noticed this when tracking pageviews one pageviews is counted as twice and i've done a lot of research regarding this and the pageviews are working good it's a very simple function that inserts a new row each time you view a page.
Things to keep in mind:
My website is inside some folders 'https://localhost/simbyone/sim/index.php'.
I don't want to use any GET variables i will have my variables from the URL string
I want .htaccess to look for existing directories located inside 'https://localhost/simbyone/sim/' and if it doesn't find any open index.php with all the strings attached something like 'localhost/simbyone/sim/blabla/' but inside index.php
everything works exactly as i said above the only thing that doesn't do right is that it double loads some pages
this is my .htaccess file:
RewriteEngine On
RewriteBase /simbyone/sim/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /simbyone/sim/index.php [L]
Thanks in advance to anyone that will help me.
Place the .htaccess inside your base folder (in your case, simbyone/sim).
Just put this in the file:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php
This will rewrite and request that is not an existing file or folder to the index.php file.
There you can use $_SERVER['REQUEST_URI'] to evalute the request (keep in mind that the prefix "/simbyone/sim/" will be present there.
Related
I am trying to use pretty links with my website, but i have a problem with paths of css and js files.
Thats what i wrote in .htaccess file
RewriteEngine on
RewriteRule ^register register.php [NC,L]
RewriteRule ^login login.php [NC,L]
RewriteRule ^settings settings.php [NC,L]
RewriteRule ^logout logout.php [NC,L]
RewriteRule ^profile/([0-9a-zA-Z]+)$ profile.php?u=$1 [NC,L]
The problem here appears when i try to access profile.php with the variable
example: http://example.com/xxxx/profile/ashraf
This here will consider profile as the main folder when importing style files which is not a folder, it's just profile.php
Thank you
I think you can add this before your rules
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
Basically what this ( common on many CMS systems ) says, is if Not a real file !-f, or a real directory !-d then continue on. Therefore if the file actually exists, such as a css file, it will not pass the condition and will not be re-written.
So I would say put it right here
RewriteEngine on
## insert here ##
RewriteRule ^register register.php [NC,L]
You may have to place it before each rule, that I am not that sure of, as I haven't done a whole lot with .htaccess in like 5 years. Basically when I learned how to use the URI instead of a URL and route everything through a index.php and a router script.
This is the extent of my .htaccess files these days ( just FYI for my explanation of my lack of remembering ) and it literally never changes. That's one of the biggest benefits of building a router and using the URI
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
This answer I did has a pretty good ( if I say so ) explanation of how what I call the URI method works.
How to change the name of the directory in url in php using htaccess?
Here is another answer I did on this topic that outlines how to build a basic router
Oop php front controller issue
Anyway hope that helps.
The browser cannot know if the paths in the URL are real directories or just parts used in rewrite rules. When it sends a request to the web server to get the CSS, the browser must convert it any relative urls to absolute and assumes the current page is in a path of directories.
In other words, in your HTML page, always use absolute urls: <link href="/xxxx/style/mycss.css" ...>
Put this after the profile.php rule. This will take out the extra 'profile'.
RewriteRule ^profile/(.*)$ $1 [L]
Or use a long path to the css file:
<link href="/xxxx/style/style.css" rel="stylesheet">
Either one should work.
I have a dynamic URL like
<domainname>/sub/cl/<phpfile.php>?c=123
phpfile.php can be any php file which comes in run time
/sub/cl/ is a folder path.
i have use this code in .htaccess:
RewriteRule ^sub/cl/new-file/([0-9]+)/?$ sub/cl/newfile.php?c=$1 [NC,L]
this is correctly redirecting to the newfile.php when i hit /sub/cl/new-file/321/ but sub/cl/ get appended before all css and js which are in root and also c=321 get lost
Please help me what m doing wrong.
Thanks in advance
if your problem is only query string insert [QSA] flag to to the rule. JS/CSS/IMG files are losing because the rewrite works for every request. Over come this issue by adding RewriteCond
RewriteCond %{REQUEST_FILENAME} !-f #Files
RewriteCond %{REQUEST_FILENAME} !-d #Directories
further reading
Apache Mod_rewrite
I was handed down a project from another developer (who's not available any more and I cannot ask him for a solution) where he'd kept multiple codeignitor projects in a folder and called each of them through their respective index files.
The file structure - /var/www/html/{folder}/mobile_index.php
The mobile_index.php references a folder 'mobile' which resides in the same folder as mobile_index.php.
So the URL that I call is - "xx.xx.xx.xx/{folder}/mobile_index.html". This page opens fine but when I click on any link (e.g. user) within the page, this redirects to "xx.xx.xx.xx/user"
Now this page is blank and my error log says there's no page user in /var/www/html/user. But when i manually convert the link to "xx.xx.xx.xx/{folder}/mobile_index.php/user" this works and opens up the page. I think the folder is missing the .htaccess file but I have no clue what to include in that file, can anyone help me get the links working again?
This is what I have so far -
RewriteEngine on
RewriteCond $1 !^(mobile_index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /{folder}/mobile_index.php/$1 [L]
Options +FollowSymLinks
RewriteEngine On
#RewriteBase / #on server, uncomment this line by removing hash'#'
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* mobile_index.php?/$0 [PT,L,QSA]
OK, first of let me just say I understand that this is a question that has been asked before. I just can't narrow it down to keywords and find what I'm looking for. So sorry in advance if this is a duplicate. htaccess rewrite is like black magic to me... I can't really get it to work.
To the question at hand:
I'm writing a simple barebone php/html site. I haven't done this in about 10 years (I usually use some CMS (wordpress, joomla etc.)). I'm trying to get a handle on some of the things that "come for free" with these CMSs. Like pretty URLs.
I have a simple index.php with some includes to build the pages. Then I have my includes folder with my dynamic content.
So two case examples of the actual URLs
index.php?page=index (my main page)
index.php?page=anotherpage (another page)
But what if I want to go to
index.php?page=a-sub-page-to-another-page
This is my PHP (index.php in web root folder)
if(isset($_GET["page"])){
$page = $_GET["page"];
$filename = "/includes/" . $page . ".php";
if(file_exists($filename)){
include("/includes/head.php");
include("/includes/navbar.php");
include $filename;
include("/includes/footer.php");
}else{
include("/includes/404.php");
}
}else{
include("/includes/head.php");
include("/includes/navbar.php");
include("/includes/index.php");
include("/includes/footer.php");
}
This is my .htaccess
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?page=$1
This works as long as I don't have any sub pages. But If I try to go to [root]/somepage/somsubpage then it doesn't work anymore. Can someone please help me out here? I'm looking to replicate the effect I get with standard CMSs like wordpress, where all URLs are SEO friendly.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?page=$1
no in you php variable $_GET['page'] will have full url for example:
example.com/foo/barr
so $_GET['page'] => /foo/barr
However this is the first part only you would need do special function to map url to your page.
do SEO pages is to store do something like: example.com/some-url/of-my-special/page-in-here.1111.html so this is your url you make it as so you need to look at .xxxxx.html xxxx is variable so now if u write htaccess like:
RewriteRule ^(.*)\.(\d+)\.html$ index.php?fullurl=$1&pageid=$2
$_GET['fullurl'] => /some-url/of-my-special/page-in-here.1111.html
$_GET['pageid'] => 1111
Please! If anyone reads this... I'm still stuck on what to do... And I
can't find anything but mysql related articles. I've searched for
days. I just need to understand how to rewrite the PHP in my original
post to work with sub-pages, dynamically with variables. Ex.
index.php?page=index, index.php?page=secondPage,
index.php?page=secondPage&SubPage <-- this is the part I can't find
anything on. How do I get the php to look for more than one level
strings?
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ index.php?level1=$1
RewriteRule ^([^/]+)\/([^/])+$ index.php?level1=$1&level2=$2
RewriteRule ^([^/]+)\/([^/])+\/([^/])+$ index.php?level1=$1&level2=$2&level3=$3
I am creating my own mvc framework to use in little projects and by default, I am rewriting the url so that every single request goes to index.php. Index.php is only 4-5 lines, which calls the application class and then, the application class calls the corresponding controller and so on.
Basically, this is my htaccess file and index.php:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?rt=$1 [L,QSA]
index.php:
<?php
include 'config.php';
$app = new Application();
?>
What I'd like to learn is whether this method has or could have any negative effects in the future in terms of speed and bandwidth. I appreciate your answers and comments.
If the class is there just to wrap you bootstrap stage, then it is pointless. simply have a plain file, which initializes application, load configuration and does all the wiring.
You could also want the index.php file to only contain one line: something that includes file outside DOCUMENT_ROOT. This way, if something goes tits-up with PHP extension, you won't show everyone your DB password and other sensitive details about your code.
As for your current .htaccess setup - no , it will not cause any additional bandwidth usage, but you might think about utilizing browser's cache for image and other assets.
Why redirect and not url rewrite?
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
as for example, this is used by a lot of applications/websites, and you have absolute control of the URL accessed.
And yes, redirecting is another call to the server.