URL Rewriting and Redirecting with folders - apache

I know there are lots of question on htaccess, yet I tried the different code I could find on Google and StackOverFlow, none worked.
I have the following in my root :
index.php
.htaccess (the one I am trying to write)
controllers
--index.php
--mycontroller.php
models
--mymodel.php
view
-index.php
--myview.php
(I am working on localhost with MAMP&Firefox)
What I have is this link
localhost:8888/MySite/controllers/mycontroller.php
What I want is
localhost:8888/MySite/mycontroller
And when I manually enter the url, I would like it to be redirected to the right controller in my MVC code
I tried this :
RewriteEngine On
RewriteBase /
RewriteRule ^/(.*)$ /controllers/$1 [L]
It doesn't redirect when I go to blabla/controllers/mycontroller.php and doesn't understand what I am asking when I manual go to blabla/mycontroller.

If your base is in /MySite/ then it needs to reflect the RewriteBase:
RewriteEngine On
RewriteBase /MySite/
# match against the php filename
RewriteCond %{REQUEST_URI} ^/Mysite/(.*)$
# check to see if the request, routed through controllers actually points to an existing file
RewriteCond %{DOCUMENT_ROOT}/MySite/controllers/%1.php -f
# rewrite
RewriteRule ^(.*)$ controllers/$1.php [L]
This should take a request for the URI: /MySite/foo and rewrite it to /MySite/controllers/foo.php if there's a foo.php file in the controllers directory.

Did this to make it work :
RewriteEngine On
RewriteBase /MySite/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ controllers/$0.php [L]

Related

rewrite problems (htaccess)

Having some experience with procedural php I watched some tutorials about OOP and the MVC model (with php). Things start to get more clear and I wanted to put the theory to practice.
The tutorial I'm following works with an app folder and a public folder, both subfolders of the root directory. There's an index.php file in the public folder and a htaccess file that redirects all requests (in the public folder) to none existing files to index php. The code in that file is:
<IfModule mod_rewrite.c>
Options -Multiviews
RewriteEngine On
RewriteBase /public
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
</IfModule>
That works fine.
In the root folder there is also a htaccess file with the purpose of redirecting all url requests to the public folder (in case /public/ is not in the url. The code in that file is:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]
</IfModule>
That goes wrong, and it's the second RewriteRule that causes the problems. If I for example browse to
www.mywebsite.com
the browser redirects to www.mywebsite.com/public/index.php
But if I browse to www.mywebssite.com/shop (shop is not an existing file) I suppose the browser redirects to www.mywebsite.com/public/index.php?url='shop', but instead there is an internal server error. It seems to be the second RewriteRule that causes the problem.
What could be the problem?
I am on mobile I haven't tested it but looks like you could be reaching out to maximum redirect limits here why because your condition in your root htaccess isn't looking good to me, try this once.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ public/ [L]
RewriteCond %{REQUEST_URI} !^/public/? [NC]
RewriteRule (.*) public/$1 [L]
</IfModule>
Issues in OP's approach: You haven't mentioned any condition to when it should redirect so it doesn't know when to stop hence it's creating a loop here IMHO.

.htaccess rewrite rule to make longer url work

I have a rewrite rule that allows me to be able to have a url like this..
http://example.com/randyt
and convert it to
http://example.com/?aff=randyt
It's working however, I also have other links that need to be used such as....
example.com/members/aff/go/randyt
example.com/members
example.com/members/signup
and so on. The rewrite rule I have ignores those urls and just goes to
example.com
Please help me add something to this so it will not ignore any url with the /members or /members/.... in it.
Here is the code I am using now...
RewriteEngine On
RewriteBase /
# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.+) ?aff=$1 [L,QSA]
Sorry about the comments section I am new here lol.
So here is the above code with the added code that someone suggested
RewriteEngine On
RewriteBase /
# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.+) ?aff=$1 [L,QSA]
RewriteCond %{REQUEST_URI} ^/members
RewriteRule . - [L]
When I try to use the extended url it still goes back to the base url showing only index.php from the root directory.
When someone uses a url like this....
example.com/members/aff/go/randyt
I need it to stay in the members directory using that affiliate code. The current program should take care of that.
Here is the .htaccess code in the members directory
<IfModule mod_rewrite.c>
RewriteEngine on
# RewriteBase /members
RewriteRule ^public public.php [L]
RewriteRule ^js.php js.php [L]
RewriteRule !\.(js|ico|gif|jpg|png|css|swf|csv|html)$ index.php
</IfModule>
<IfModule mod_php5.c>
# php_flag magic_quotes_gpc off
</IfModule>
That code was pre-written by the company I purchased the program from.
So to sum this up, I need 2 different things to happen...
If someone uses:
example.com/randyt (randyt can be any username)
it needs to stay on example.com showing that page (index.php file)
If someone uses:
example.com/members/aff/go/randyt (again randyt can be any username)
it needs to stay in the members directory and use the purchased script.
Try this rule in /members/.htaccess:
DirectoryIndex index.php
RewriteEngine on
RewriteBase /members/
RewriteRule ^public/?$ public.php [L,NC]
# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
You can add a condition to your .htaccess
RewriteCond %{REQUEST_URI} ^/members
RewriteRule . - [L]
This condition will check if the Requested URI contains a string starting with /member then it will be passed unchanged to its destination.

mod_rewrite in a subdirectory

Our web server is structured as follows
/path-to-docdir [Contains main pages]
./app/ [Some application not directly related to the main content]
The main pages are served fine. In the app directory I am trying to use mod_rewrite to redirect all pages to an index.php in a public directory
So a request to
/server/app/start/login/ => /server/app/public/index.php
/server/app/start/login => /server/app/public/index.php
/server/app/start/login?name=someone => /server/app/public/index.php?name=someone
The front controller relies on the last two components of the REQUEST_URI string to interpret controller and action.
To do the above I have created the following .htaccess file in the app directory
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^$ public/ [L]
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
Here, I find that the server goes in a redirect loop for the above URLs. I tried replacing $1 with index.php and that does bring up the page, but the internal references within the page to script files run into errors.
After some searching around, I added the following line:
RewriteBase /app/
And that did not seem to make any difference. I should also note that everything works as expected if I make the app directory as the server root directory.
I've also turned on debug logging and all I could gather from that was that the server was indeed going into a redirect loop.
[perdir /path-to-docdir/app/] internal redirect with /app/public/public/public/public/public/public/public/public/public/public/public/start/login/ [INTERNAL REDIRECT]
I'm not really sure where to start looking at this point, so any help or pointers to move me further would be much appreciated.
Edit:
This htaccess file worked for me - if anyone else is looking for a solution. Apparently, I was missing rewrite conditions
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ public/index.php?$1 [L]
</IfModule>
A few points:
A RewriteBase path does not need a trailing slash, so RewriteBase /app/ should be RewriteBase /app.
RewriteRule ^(.*)$ public/$1 makes RewriteRule ^$ public/ superfluous. The former will match http://yourserver/app and internally rewrite as http://yourserver/app/public, so there's no need for the latter.
Finally, try adding a rewrite condition:
RewriteEngine On
RewriteBase /app
RewriteCond %{REQUEST_URI} !^/app/public/
RewriteRule ^(.*)$ public/$1 [L]

how do i rewrite URL such as

I need to rewrite my web URL such as
http://www.site.com/profile/ -> profile.php
http://www.site.com/settings/ -> settings.php
and another word of profile and settinhs is link to user.php
example,
if I push http://www.site.com/giffary/ - it's link to user.php?name=giffary
but when i push http://www.site.com/profile/ - it's link to profile.php not user.php.
how can i write .htaccess file
thank you :)
You will need to use mod_rewrite for this, obviously. I have not tested it, but this config should work:
RewriteEngine On
RewriteBase /
# Ignore URLs that point to files/directories that actually exist.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Special rewrites
RewriteRule ^/profile/?$ /profile.php [L]
RewriteRule ^/settings/?$ /settings.php [L]
# User profile rewrites
RewriteRule ^/([^/]+)/?$ /user.php?name=$1 [L]

Remove ".html" from URL via .htaccess for a WordPress website

Background information:
I've searched stackoverflow for a specific solution and couldn't find one that fixed my situation. Thanks in advance for any help you can offer. Your knowledge is appreciated.
I've decided to accept a contract to "convert" (in the client's words) a Joomla site into a WordPress site. Everything is going along smoothly, except that the Joomla site links to .html files, both in its navigation and in the content of 100+ posts.
Instead of going through each post one-by-one and updating the links or running a SQL command to remove ".html" from URLs, I've decided to put the pressure on .htaccess, with which I am somewhat comfortable.
What I'm trying to do ↓
In WordPress, I have custom permalinks enabled, and it is as follows:
/%category%/%postname%
Here's an example of what one of the old URLs in the posts looks like:
http://the-site.com/category/the-webpage.html
I need the htaccess file to tell the webserver to remove the .html so the user, after visiting "http://the-site.com/the-webpage.html" is instead sent to:
http://the-site.com/category/the-webpage
I'm setting up the page stubs to follow the file name of the Joomla pages, so http://the-site.com/category/the-webpage will work.
My question:
Can you help me discover the solution to removing .html from the URL when someone visits the site, even if the HTML file doesn't exist on the server?
Here's how the .htaccess file looked before I made changes:
# 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
Here's the latest .htaccess file as of 5:35pm Eastern:
# BEGIN WordPress
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} \.html$
RewriteRule ^(.*)\.html$ $1 [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress
The ↑latest .htaccess changes work. Thanks Tim!
This will work to force an external redirection to your new URLs, but this may not be ideal for your situation. I'm still trying to think if there's a way to keep the redirection internal and update the variable that WordPress uses to determine which page to serve up, but so far I haven't thought of anything that would work.
Entire .htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} \.html$
RewriteRule ^(.*)\.html$ $1 [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
You want to use a URL rewrite
RewriteEngine On
RewriteRule ^(.*)\.html$ $1
This should do it. It will rewrite a request to site.com/category/whatever.html to site.com/category/whatever. it shouldn't be dependent upon the requested file existing.
<Directory /var/www/category>
RewriteEngine on
RewriteRule (.*)\.html$ /category/$1
</Directory>
This is the format for apache2.conf or virtual host files. Not sure if you use the command in .htaccess. It's best to take care of it in the server conf, if you can, as that is only parsed once, on server startup, and htaccess is parsed on each request.