I have a custom PHP CMS website.
https://example.com/
I have been able to remove the .php extension for https://example.com/archives for example.
But when it comes to:
https://example.com/single-article.php?getid=1425
I have tried EVERYTHING the code just won't change in the .htaccess.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^single-article/([0-9]+)/([09-9a-zA-Z_0]+) single-article.php?getid=$1
Any ideas?
Related
Site Structure
/articles/Employment/Companies.php
/articles/Employment/Companies/.htaccess
/articles/Employment/Companies/index.php
.htaccess file reads
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/(.*)$ index.php [L]
So when you go to
/articles/Employment/Companies/[company type]
It is displaying the index.php page.
The Problem
I'm trying to link to
/articles/Employment/Companies.php
without the .php being displayed, however if I link to
/articles/Employment/Companies
it is going to
/articles/Employment/Companies/
What i'm Ideally Looking For
Understand why I my site is adding the / when linking to folder/hello
to strip out all .php so if you go to /hello it'll display /hello.php apart from in certain directories such as my current .htaccess file is located where /this or /that will display /index.php.
Please try with below, use from rewritecond with your existing rule what I am doing if the request is actually for php file which is not index.php then serve the extension less code.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule !index.php$ $1.php [L]
I am working on a site where I have many short URLs which is redirected to a bigger version of URL. The idea is that the short URLs are distributed offline and users generally remembers them and types in the browser address bar. As example:
http://www.example.com/abc should redirect to http://www.example.com/higher_education/companion/politics/abc.html
Also there are some external links as well. like: http://www.example.com/google will redirect user to https://www.google.com
EDIT -
There is another scenario where http://www.example.com/elementary/def.html will be redirected to http://www.example.com/elementary/xyz.html
However, we now want to achieve this with RewriteMap. The code that we've written for that is as below:
<IfModule rewrite_module>
RewriteEngine on
RewriteMap custommap txt:/var/www/vhosts/example.com/httpdocs/map.txt
#RewriteCond %{REQUEST_URI} !^/index.php$
RewriteRule "^(.*)$" "${custommap:$0}" [R,L,NC]
</IfModule>
The map.txt contains:
abc http://www.example.com/higher_education/companion/politics/abc.html
google https://www.google.com
The problem is .htaccss file contains also some RewriteRule.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*)\.html$ /index.php?sid=$1&ssid=$2 [L,NC,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.html$ /index.php?sid=$1 [L,NC,QSA]
So any request comes with .html will be served by index.php as standard CMS feature. However, now the code goes into redirect loop.
If someone shares some light in order to mitigate this issue then that would be great. The Apache version is 2.2 so I can't use any IF-ELSE in that.
Regards,
Aneek
You should put some restrictions on the rule serving URLs from RewriteMap:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond ${custommap:$0} !^$
RewriteRule ^[^.]+$ ${custommap:$0} [R=301,L,NE]
This will ensure only non-files and non-directories with no DOT in URI will be handled by custommap.
More importantly it will also check existence of a mapping in custommap before redirecting.
Make sure you clear your browser cache before testing this change.
I am trying to redirect a bunch of old blog article URLs using the .htaccess file:
RedirectMatch ^/index\.php/global/article/(.*)$ http://www.mywebsite.com/blog/article/$1
This doesn't really work, however, because my CMS seems to get confused by the index.php bit and keeps adding ?symphony-page= to all the URLs.
It's probably this part that is responsible:
### FRONTEND REWRITE - Will ignore files and folders
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*\/?)$ index.php?symphony-page=$1&%{QUERY_STRING} [L]
Can anybody help?
Please try the following (comments included to explain):
RewriteEngine On
# First, redirect the old URIs to the new ones via a 301 redirect.
# This will allow the CMS to take over using the rules that follow.
RewriteRule ^index.php/global/article/(.+)$ /blog/article/$1 [L,R=301]
# Frontend Rewrites - for the CMS
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*\/?)$ index.php?symphony-page=$1&%{QUERY_STRING} [L]
Try this:
#-- Input URL >> http://www.mywebsite.com/index.php/global/article/abc
### FRONTEND REWRITE - Will ignore files and folders
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*\/?)$ index.php?symphony-page=$1&%{QUERY_STRING}
RewriteCond %{REQUEST_URI} ^/index\.php/global/article/(.*)$
RewriteRule ^.*$ blog/article/%1 [R=301, L]
#--Output URL >> http://www.mywebsite.com/blog/article/abc
I've tested the above with this htaccess.madewithlove and it seems like it will work just fine, hopefully it will work for you too
Screenshot:
i got a site php based with some rules in the .htaccess file to get rid of file extentions in the url adress bar. Basically it takes http://netbureau.com.br/en/about.php/ and turns it into http://netbureau.com.br/en/about.
Here are the lines in the htaccess file:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
The problem comes when i try to access the rss feed of the blog at http://netbureau.com.br/blog/?feed=rss2 and when i try to set custom permalinks for the blog at http://netbureau.com.br/blog. It gets messed up by the htaccess file.
So is there any way to disallow the RewriteRule for the /blog folder so that i can get back my rss link and set custom permalinks in the blog?
I know it's at the same time Wordpress related but it feels more connected to the htaccess file than Wp.
Thanks in advance.
EDIT #1:
I've set up the wordpress permalinks to the default structure which goes like this: http://netbureau.com.br/blog/?p=123 This made my rss link back for good.
The remaining problem is that Wordpress gives me its own rewriterule which is:
RewriteEngine On
RewriteBase /blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
Is there a way to to still use the first rule to apply to the whole site except the /blo/ folder and apply the WP rule only to the /blog/ folder?
I've tried different combinations but without luck so far. I could only have the site without the custom links for the blog or the custom blog links and a 404 on the pages of the site.
Try:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/blog/?
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
The !^/blog/? expression excludes any URI that starts with /blog
I have been trying forever to get rid of the .php from my site. I want it to look like example.com/about instead of example.com/about.php. I have it to were it will rewrite it so i can type it in as example.com/about but i want it to were when i click on a link on my website to go to example.com/about it loads the url as example.com/about and not example.com/about.php. Overall i would like it so that whenever someone loads anything on my page the end of the url doesnt have the .php. My .htaccess right now looks like so,
RewriteEngine on
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)/$ $1.php
Thank you.
Try this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
You wouldn't have to change your site's structure as it simply targets "malformed" URIs in the form of "http://x.com/page" without an extension at the end. Hope I helped!
but i want it to were when i click on a link on my website to go to example.com/about it loads the url as example.com/about and not example.com/about.php
To fix that, you need to change links from your site from example.com/about.php to example.com/about. You can still do a redirect when links "in the wild" still point to the .php URLs, but unless you change the links on your site to remove the extension, your're causing every page load to hit your server twice.
As for the redirect:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.+)\.php[\?\ ]
RewriteRule ^ /%1/ [L,R=301]
I'm not sure how your rules to put the php extension back is even working, since you're matching for a trailing slash but when you check the file with -f, you'll have a stray trailing slash in your filename: e.g. /some/path/to/the_file/.php instead of /some/path/to/the_file.php
Try:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/(.+?)/?
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^ /%1.php [L]