Mod_rewrite URLs for SEO - apache

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

Related

URL Rewriting Add a folder name to URLs

I have an apache2 server.
My directory structure is - (below is inside - /some-folder/abc)
- app
- otherfolder
- pqr.php
- xyz.php
- js
- css
.htaccess is placed inside /some-folder/abc
Context root for virtual host is set till - /some-folder/
Now, I want my users to enter this URL - http://someserver.com/abc/xyz or http://someserver.com/abc/pqr
I want to open the page at - http://someserver.com/abc/app/xyz.php or http://someserver.com/abc/app/pqr.php
How can I achieve this using URL Rewriting mod_rewrite
This is what I have so far, which is not working -
Options +FollowSymLinks -MultiViews
#Turn mod_rewrite on
RewriteEngine On
#RewriteBase /abc
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ /app/$1\.php [L] # this doesn't work either
# RewriteRule ^(.*)$ app/$1.php [L] # this doesn't work either
# RewriteRule ^abc/(.*)$ abc/app/$1.php [L] # this doesn't work either
Thank you for your help.
If possible, I would also like to take all query params using forward slash
ex - http://someserver.com/abc/app/xyz.php/xId/123/uId/938/sdh/8374 instead of http://someserver.com/abc/app/xyz.php?xId=123?uId=938?sdh=8374
There is no pattern for query params, they can be anything for an page. Is this possible by a generic mod_rewrite, or do I have to rewrite urls for each page, and each time my developers add a new query param?
This rule should work for you from /abc/.htaccess:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/abc/app/$1.php -f
RewriteRule ^(?!internal)(.+?)/?$ app/$1.php [L,NC]
RewriteCond makes sure that we have a corresponding .php file in /abc/app/ sub-directory.
(?!internal) is negative lookahead assertion to skip internal from this rewrite.
Also it appears you're using a relative URL for css/js/images e.g. src="abc.png" and your current URL is: /abc/xyz then browser resolves this relative URL to http://example.com/abc/xyz/abc.png which obviously will cause a 404 since your static files are residing in /abc/app/ sub-directory.
You can add this just below <head> section of your page's HTML:
<base href="/abc/app/" />
so that every relative URL is resolved from that base URL and not from the current page's URL.
Also as a practice to use absolute links instead of relative ones change relative link XYZ, to this one XYZ

.htaccess setting for handling index files, trailing slashes, and file extensions

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 .

htaccess redirect subdirectory (folder) to single page in same subdirectory

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.

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

mod_rewrite -> doesn't work as expected

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.