How to use mod_rewrite and edit URL path? - php

I apologize to ask such a question, there's hundreds of them all around, and I wouldn't ask if I didn't need to understand. I've tried countless times on .htaccess but unable to make it work, I'm also testing on local server but I'm sure mod_rewrite is on, so should be errors I'm making.
I'm currently trying to change m URL from
example.com/article.php?article_id=article-id-goes-here&article_title=article-title-goes-here
to
example.com/article/75/article-title-goes-here
The latest code I've tried was the following:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/$ webroot/article.php?article_id=$1&article_title=$2 [QSA,L]
My folder structure currently stands as
blog/
libraries/
articleClass
articleClass.php
webroot/
css/
images/
js/
.htaccess
I forward everything to webroot as that's my index page.

You have an extra slash at the end of your pattern that isn't present in the URL that you say you are using:
RewriteRule ^([^/]+)/([^/]+)/?$ webroot/article.php?article_id=$1&article_title=$2 [QSA,L]
You can make is optional by adding a ? after it.

example.com/article.php?article_id=article-id-goes-here&article_title=article-title-goes-here
to
example.com/article/75/article-title-goes-here
Try this rule:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^article/([^/]+)/([^/]+)/?$ webroot/article.php?article_id=$1&article_title=$2 [QSA,L,NC]

Related

.htaccess redirect all urls to public/index.php without backslash on localhost

Background
I'm creating a time tracking app with PHP on my localhost (MAMP). The app structure is as follows
htdocs/time-tracker/public/index.php
Issue
No matter how many configurations I try, I can't seem to avoid some sort of weird glitch with the URL.
What I need
I want the following result. When I visit the url 127.0.0.1:8888/time-tracker on my local machine, I trigger the php app, routing all requests through the htdocs/time-tracker/public/index.php. Preferably without a trailing slash, but priority is just to get the app to work.
My current .htaccess file
RewriteEngine On
RewriteBase /time-tracker/
RewriteRule ^public\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /public [L]
Updates
1. $_GET parameters change outcome
For some reason http://127.0.0.1:8888/time-tracker?debug=true and http://127.0.0.1:8888/time-tracker get me different results.
http://127.0.0.1:8888/time-tracker?debug=true results in a redirect to http://127.0.0.1:8888/public
http://127.0.0.1:8888/time-tracker results in a redirect to http://127.0.0.1:8888/Users/etc/etc/htdocs/time-tracker/public
Neither of these results are what I want.
2. Partially working
This .htaccess file has gotten my redirects to work whenever I put something in the $_GET
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ public/index.php [L]
For example, 127.0.0.1:8888/time-tracker/?test=test works while 127.0.0.1:8888/time-tracker/ still redirects to http://127.0.0.1:8888/Users/etc/etc/htdocs/time-tracker/public
3. Not redirecting properly on root
The redirects works on all paths except for the root path. For example, 127.0.0.1:8888/time-tracker/test and 127.0.0.1:8888/time-tracker/?test=test both work, just not 127.0.0.1:8888/time-tracker/
I don't know why my regex won't pick this up. Here is my code
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* public/index.php [L]
4. Seeing an empty path name
I've tracked it down to one last issue: empty paths don't register with the redirect.
# Settings
Options +FollowSymLinks
DirectorySlash Off
RewriteEngine On
# Rules
RewriteBase /time-tracker/public/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*|x) index.php?/var=$1 [L]
For some reason, it just can't catch the redirect if the path is empty.
5. Close enough solution
This is the best I got. This is actually working, but I couldn't fix the trailing slash issue.
# Settings
DirectorySlash On
RewriteEngine On
# Rewrite
RewriteBase /time-tracker/public/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) index.php?/var=$1 [L]
Hopefully somebody can come solve the trailing slash directory root issue or at least confirm that it is impossible. At this point, the correct answer goes to anyone who can explain my mistakes and make this into a helpful post.
Try this right after RewriteEngine on
RewriteRule ^(.*)/$ /$1 [L,R=301]
Try this. Put your .htaccess file in the time-tracker folder
RewriteOptions inherit
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /time-tracker/public/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>

.htaccess not working with php includes

I'm trying do use mod_rewrite at my .htaccess but isn't working.
my url is http://gestor.samfbas.com.br/index.php?p=something
it should be http://gestor.samfbas.com.br/something
The file is in a subdirectory (gestor) in my host, where all the files are.
<IfModule mod_rewrite.c>
Options +FollowSymLinks +Indexes
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/?$ /index.php?p=$1 [L]
</IfModule>
Try this one (if index.php is in the root folder http://gestor.samfbas.com.br/index.php):
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/index.php
RewriteRule ^(.*)$ index.php?p=$1 [L,QSA]
This should work. Testet it here on my local maschine (No other server redirects or else, just a fresh xampp installation).
It redirect http://gestor.samfbas.com.br/something to http://gestor.samfbas.com.br/index.php?p=something without changing the url in the browser.
And additional to the question in the comment.
This URL part p= should not be known be outside users!
Better use a long var here like sadff34dngn4nil212ugn=, so nobody can call the index.php with parameters directly from outside. You can't prevent that 100% but the redirect parameter p= is only for internal use.
But its just my opinion on that.
Hopefully that helps a little.
Find the right way to rome ;)

Remove sub folder url in htaccess

I have got it right to remove my subfolder from the url but its making my other root sub folders not pick up.
Eg...
www.blah.com/templates/index.html is now www.blah.com/index.php
but my www.blah.com/systemfiles/signin.php gives an error
I also have www.blah.com/systemfile/signin.php.
I can not access other sub folders.
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/templates
RewriteRule ^(.*)$ templates/$1 [L]
The four lines you added says:
Follow symlinks
Start the enginge
If not /templates is in the URL
Treat the URL as /template was added
That is why it does not work for you. This strategy is kind of weird... But to make it work with your example you should check if the url points to a file or folder.
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/templates
RewriteRule ^(.*)$ templates/$1 [L]

Correct way to remove index.php in URLs

I'm currently learning fuel php and I've searched a lot in google all day long and still can't find a solution on how to remove index.php in URLs. That is, instead of
localhost/project/public/index.php/hello
I only need to write:
localhost/project/public/hello
I'm currently doing the tutorial here and when I clone their repository I'm getting a 404 error not found due to there URL having no index.php. How can I do that? I'm using WAMP server.
Create an .htaccess in the top level directory and add:
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^ index.php [L]
</IfModule>
Try to add this .htaccess file to your web directory:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^(.*)$ index.php [NC,L]

Keep URL the same, load index.php

My question might be dumb, but I googled it and didn't find an answer...
Let's say I want to access my website in this given url: www.mywebsite.com/something/something_else/?some_query_string=true
(I put the query string because it is going to be there, and I don't know if it makes any difference in the htaccess file)
I want it to keep the URL the same, but load the index file for no matter what URL, which is not in the root of the server.
My server has an "application" folder, where all the code is.
How can I do this?
Thanks!
use htaccess to re-write all request to index.php (except for when a file/dir/link is requested and it exists):
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* index.php [L]
</IfModule>
If you want to use Rewrite to have your requests handled by a file outside the DocumentRoot, then you can combine with an Alias directive.
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* application/index.php [PT]
Alias application /path/to/application
Note the [PT] on the RewriteRule which means 'pass through' - it ensures the rewritten url is passed through to other apache modules which might be interesting in processing it.
This turned out to answer my own question:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ application/index.php?url=$1 [QSA,L]
If the URL doesn't exist, it loads the index.php file inside of the folder "application", and it keeps the URL the same, which was exactly what I needed...
Thanks for the answers!

Categories