URL redirects in htaccess - php

I have a htaccess file that is set and running on a live server. Everything is functioning fine with it but I have to add some specific URL re-directs to it. The specific re-directs had been setup in an IIS web.config file. An example:
<rule name="URL re-direct" stopProcessing="true">
<match url="^program_consumer((\.cfm)|(\.php)|()+)((\?.*)|())$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
<action type="Redirect" url="site_programs{R:7}" />
</rule>
I'm having a hard time translating this to mt htaccess file. I've tried:
AddDefaultCharset OFF
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
# Resolve .php file for extensionless php urls
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
# Re-direct Non-file Non-directories
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([program_consumer]+)((\.cfm)|(\.php)|()+)((\?.*)|())$ http://{HTTP_HOST}/site_programs$1 [L,R=301]
Obviously, the program_consumer is not a file that exists anywhere in the root document directory, but the site_programs does exist and is served as an extension-less PHP file. Any thoughts on what I'm missing?

Your regex in RewriteRule seems faulty and it needs to be placed before internal rewrite rule:
AddDefaultCharset OFF
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^(program_consumer\.(?:cfm|php))$ /site_programs/$1 [L,R=301,NC,NE]
# Resolve .php file for extensionless php urls
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

Related

How to use .htaccess rewrite to completely rewrite the URL

I'm trying to imitate a routing effect. In my setup the login.php file is located under localhost/session/login.php. I've already found out how to omit the .php file ending but that's not enough for me. Here is my .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
This changes the URL to localhost/session/login but what I really need it an URL like this: localhost/login.. The trouble is that I don't really understand how the rewrite above works and I'm getting a lot of mixed answers.. don't know what to do exactly.
Is there a way to have my URL be localhost/login by using some RewriteCondition?
You may use following rules in your site domain .htaccess to enable use example.com/login rewriting to example.com/session/login.php:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/session/$1.php -f
RewriteRule ^(.+?)/?$ session/$1.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+)/?$ $1.php [L]
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess.
Make sure to put this .htaccess in DOCUMENT_ROOT directory.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /session/$1 [L]
</IfModule>
Flags used are,
L - Last
This will serve the file at location localhost/session/login.php to localhost/login.php

.htaccess help within root and public

I have a small mvc application where I'm trying to force the url to go from www.example.com to www.example.com/public.
In order to do this, I have a .htaccess file in the root (/) and then another .htaccess within the /public folder to point to index.php with friendly urls. I believe the issue is within the htaccess file that sits in the public folder, I need all requests to go through index.php
I have tried a few threads on stackoverflow but seems to give me a server 500 error when I access www.example.com
.htaccess of root
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /public/
</IfModule>
.htaccess of public folder
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}% !-f
RewriteCond %{REQUEST_FILENAME}% !-d
#allow images, css and js links
RewriteCond %{REQUEST_URI} !\.(css|js|jpe?g|gif|png)$ [NC]
RewriteRule ^(.*)$ index.php?url=$1 [PT,L]
</IfModule>
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/index.php
RewriteRule (.*) /public/index.php

Wrong redirecting but only standard WP-Rules in .htaccess

Problem:
I have a download link with an ip as "site name"
http://192.168.1.1/downloads/file.txt
This is redirecting to
https://www.mywebsite.comdownloads/file.txt
Now I want it of course to redirect correctly to
https://www.mywebsite.com/downloads/file.txt
What I've checked:
I've checked some WP options. There was nothing obvious.. I've checked the .htacess. There were only the standard WP rules:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Any ideas? Is there maybe another file where I can set rules?
Check this rewrite in .htaccess file (before #BEGIN WordPress rules)
RewriteEngine on
RewriteCond %{HTTP_HOST} ^192\.168\.1\.1
RewriteCond %{REQUEST_URI} ^\/downloads\/file.txt$
RewriteRule ^(.*)$ https://www.mywebsite.com/downloads/file.txt [L,R=301]

How to configure .htaccess for custom urls

I need to know how to set up .htaccess to make custom URL as follows:
"www.examplesite.com/main/sub/sub_sub"
i have the follow code in my .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# Removes index.php from ExpressionEngine URLs
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteCond %{REQUEST_URI} !http://localhost/routeB/.* [NC]
RewriteRule (.*?)index\.php/*(.*) routeB/$1$2 [R=301,NE,L]
# Directs all EE web requests through the site index file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ routeB/index.php/$1 [L]
</IfModule>
Help me please!
i'm making a framework

Help with htaccess RewriteRules

I am posting this question again because I didn't explain clear enough. Here are things that I need to achieve with the .htaccess file:
Re-route /public_html/ to /public_html/myfolder/
Make website/index.php?q=param to website/param/
My php files are inside /public_html/myfolder/ and my image files are inside /public_html/myfolder/images/
Options -Indexes
RewriteEngine on
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} !^www\.website\.com$ [NC]
RewriteRule .* https://www.website.com/$1 [L,R=301]
RewriteRule ^$ myfolder/index.php [L]
# Rewrite rules
<IfModule mod_rewrite.c>
#RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ myfolder/index.php?q=$1 [L,QSA]
</IfModule>
The problem is, it redirects fine.. but I can't see any images! It doesn't seem to load my js, css, and etc..
This is the error I get:
1<br />
2<b>Notice</b>: Undefined property: stdClass::$component in <b>/home/website/mypage/includes/Template.class.php</b> on line <b>31</b><br />
I would do something like this. I'm thinking the problem could be the order of the rules, that say, the first rule will try to load the image/js/css since isn't looking if the file exists.
try this:
Options -Indexes
RewriteEngine on
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} !^www\.website\.com$ [NC]
RewriteRule .* https://www.website.com/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ myfolder/index.php?q=$1 [L,QSA]
RewriteRule ^$ myfolder/index.php [L]

Categories