I have updated my .htaccess file to remove the .php extension from the url strings. Code is here:
ErrorDocument 404 /lost.php
RewriteEngine On
RewriteBase /
# remove enter code here.php; use THE_REQUEST to prevent infinite loops
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
RewriteRule (.*)\.php$ $1 [R=301]
# remove index
RewriteRule (.*)/index$ $1/ [R=301]
# remove slash if not directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/ $1 [R=301]
# add .php to access file, but don't redirect
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
Everything is working as it should do and the .php is being removed. Only problem is that when I try and access a certain area of the site (client area) the page is not displaying at all properly. Screen shot below:
Before adding the above code into the .htaccess file the url is:
www.mysite.co.uk/client-area.php
After adding the above code into the .htaccess file the url is:
www.mysite.co.uk/client-area/
and the page is not displayed at all correctly? Any idea why this is happening and what needs to be done to the .htaccess file code to fix this? Any help is welcome : )
Keep your .htaccess code like this:
ErrorDocument 404 /lost.php
RewriteEngine On
RewriteBase /
# remove enter code here.php; use THE_REQUEST to prevent infinite loops
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} \s/+(?:index)?(.*?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]
# remove index
RewriteCond %{REQUEST_METHOD} !POST
RewriteRule (.*)/index$ $1/ [R=301,L,NE]
# remove slash if not directory
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/$ $1 [R=301,L,NE]
# add .php to access file, but don't redirect
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]
For displaying css/js/images just use absolute path in your css, js, images files rather than a relative one. Which means you have to make sure path of these files start either with http:// or a slash /.
Alternatively you can try adding this in your page's HTML header: <base href="/" /> so that every relative URL is resolved from that URL and not the current URL.
Related
I tried to modify my .htaccess file for SEO and do not seem to understand how I can prevent web crawlers to grab double content from my website. Because it seems like Google is indexing my website in two manners:
https://www.example.com/ AND
https://www.example.com/index.php
This is the .htaccess-Code
RewriteEngine On
DirectoryIndex index.php
RewriteBase /
# remove .php; use THE_REQUEST to prevent infinite loops
RewriteCond %{HTTP_HOST} ^www\.get-to-med\.com
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
RewriteRule (.*)\.php$ $1 [R=301]
# remove index
RewriteRule (.*)index$ $1 [R=301]
# remove slash if not directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/ $1 [R=301]
# add .php to access file, but don't redirect
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
RewriteCond %{HTTP_HOST} ^get-to-med.com
RewriteRule (.*) https://www.get-to-med.com/$1 [R=301,L]
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
ErrorDocument 404 /diese-seite-existiert-nicht.php
Instead of messing with the .htaccess file endlessly, why not add a canonical tag in the index.php file to the desired version of the page:
<link rel="canonical" href="https://www.example.com/" />
Any search engines following the canonical rule will act accordingly.
As for the .htaccess issue, you've got DirectoryIndex index.php in there so that probably has something to do with it (any time the / directory is requested, the index.php file will be returned; may be stored by Google as such).
My request is quite simple. Using my current .htaccess conditions and rules as given here:
# Remove .php extension from URLS
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
# Redirect from *.php to URL without *.php
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]
Problem is, when I pass a URL that contains *.php?param1=A¶m2=B as a parameter, it throws away "?param1=A¶m2=B"
For example:
I want to redirect to: "/views/users/login.php?redirect=/views/home.php?id=1"
Resulting in: "/views/users/login?redirect=/views/home", which throws away "?id=1", so now I can not access that parameter.
How do I rewrite my rules so that it keeps those parameters?
Any suggestions are welcome and much appreciated.
Update (2015-09-16):
Removing
RewriteRule ^(.*)\/index\.php$ $1 [R=301,L,NC]
As it is irrelevant.
You'll have to use urlencode to encode the URL into a parameter.
So when building the link or redirect, use:
redirect('views/user/login.php?redirect=' . urlencode('/views/home.php?id=1'))
btw: redirecting to a "controller" in a folder called "views" might be a bit confusing in a few month :)
Hello you just need to add the query append marker like so:
RewriteRule ^(.*)\/index\.php$ $1 [QSA,R=301,L,NC]
that is "QSA"
I have solved this issue (to some agree) using the following code:
# Enable rewrite mod.
RewriteEngine On
RewriteBase /
Options +FollowSymLinks -MultiViews
# Redirect URLs that contain *.php to extensionless php URLs.
RewriteCond %{THE_REQUEST} ^(.*)\.php
RewriteRule ^(.+)\.php$ $1 [R,L]
# Resolve *.php file for extensionless php URLs.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*)$ $1.php [NC,L,QSA]
# Resolve /views/* for URLs that do not contain views and needs it.
RewriteCond %{REQUEST_URI} ^/applications/(.*)$ [OR]
...
RewriteCond %{REQUEST_URI} ^/home [OR]
...
RewriteRule ^(.*)$ /views/$1
# Redirect URLs that contains /views/* to viewsless URLs.
RewriteCond %{THE_REQUEST} ^(.*)/views/(.*)
RewriteRule ^views(.*)$ $1 [R,L]
I'm rewriting my portfolio website urls by using the following code in a htaccess file,
RewriteEngine On
RewriteBase /
#remove enter code here.php; use THE_REQUEST to prevent infinite loops
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
RewriteRule (.*)\.php$ $1 [R=301]
# remove index
RewriteRule (.*)/index$ $1/ [R=301]
# remove slash if not directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/ $1 [R=301]
# add .php to access file, but don't redirect
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
It all works until I want to navigate to another page from the blog-post.php page
for example, let say I want to go to the about page the url becomes cgarcia.design/web/about and it should be cgarcia.design/about so the page can load properly. Now what would I need to change in the htaccess file to accommodate files within folders?
my nav structure is the following
work
about
resume
blog - posts folder - entry
contact
Thank you for any suggestions.
Add this to the ned of your document:
RewriteRule ^$ web/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ web/$1
I have files in my root folder. jobs.php, contact.php.
I want to be able to use .htaccess to remove the .php extension and force a trailing slash /. So that, www.domain.com/jobs/ and www.domain.com/contact/ goes to their respective pages.
Also, I want jobs.domain.com to point to www.domain.com/jobs/
<IfModule mod_rewrite.c>
Options +SymLinksIfOwnerMatch
RewriteEngine On
RewriteBase /
RewriteRule ^(rosquillos)/$ $1 [L,R=301,NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
</IfModule>
This is what I've got so far, and as much as I try. I can't seem to get it to work.
Any help would be awesome.
EDIT:
I would like to rephrase my question. The existing code removes the .php extensions for me, what I really only need right now is an additional rule that points the subdomain to the correct file: ie. jobs.domain.com points to jobs.php. I can live without the trailing slash.
Your condition:
RewriteCond %{REQUEST_FILENAME}\.php -f
will fail if your URL's end with a trailing slash. But if you only have jobs and contact then you'll just want:
RewriteEngine On
# add trailing slash and redirect browser
RewriteRule ^([^/]+)$ /$1/ [L,R=301]
RewriteRule ^([^/]+)/$ /$1.php [L]
# redirect direct access to php files:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+)\.php
RewriteRule ^ /%1/ [L,R=301]
Oh and I forgot:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com$ [NC]
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^ /%1.php [L]
Assuming that *.domain.com has the same document root as www.domain.com.
I'm trying to remove the .php extensions from a site, which I have gotten to work. example.com/parent/child.php correctly changes to example.com/parent/child/
However, when going to example.com/parent.php it still rewrites properly but shows an index page.
I'm fairly new to .htaccess and Apache, so any help would be greatly appreciated!
Update from the comments
"My apologies, Its not letting me write the code. I grabbed it from this post:
Remove .php extension (explicitly written) for friendly URL"
RewriteEngine On
RewriteBase /
# remove enter code here.php; use THE_REQUEST to prevent infinite loops
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
RewriteRule (.*)\.php$ $1 [R=301]
# remove index
RewriteRule (.*)/index$ $1/ [R=301]
# remove slash if not directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/ $1 [R=301]
# add .php to access file, but don't redirect
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]