Routing from slug url using htaccess - php

I'm currently writing a PHP application, and I have always used frameworks and always worked in larger projects, but for this particular project I am doing neither. I've hit an issue where if I go to:
http://localhost/project
It runs the index.php file fine, but if I wanted to go to
http://localhost/project/test
I'd still want it to run the index.php file, and work out what to do with the 'test' part from there. Instead I'm getting a 404. How do I go about this?
I'm using Apache as my server, is it something I need to change in the .htaccess file? I'm pretty new to editing that file and have found most tutorials haven't said how to do this, but it seems like a pretty integral thing
edit:
This is what my htaccess currently looks like
Options +FollowSymLinks
IndexIgnore */*
# Turn on the RewriteEngine
RewriteEngine On
# Rules
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php

Try to change your .htaccess file into :
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*)(.*)/?$ index.php
as initial solution to check if it works

Related

htaccess and pretty links

I'm trying to write a simple blog which is working fine untill i get to the pretty links thing.
what i'm looking to do is use URLs like mysite.com/blog/this_post and pass that into the index.html file as a url parameter so index.html?blog=this_post
Been searching everywhere and found a bunch of htaccess code but most didn't work. I did find one solution that tries to work but for some reason isn't doing it correctly. (with and without the Options +FollowSymLinks part)
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^blog/(.*) /blog/index.html?blog=$1
when I use this i get /blog/index.html?blog=index.html in the url instead of keeping the /blog/this_post and porting that into the index.html as expected. Could something on the server be set incorrectly? everything else with the blog is working like a charm but so nothing wrong with the script its just getting the pretty links to work.
Looked at an older site that has wordpress and coppied over that htaccess code but it does that same thing. All the other searches on here and other sites point to the same solution above or the wordpress but for some reason it's passing index.html to the script and not the last url segment "this_post" as expected.
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.html [L]
</IfModule>
# END WordPress
what should happen is the user types in
mysite.com/blog/this_post
which remains in the address bar correct? but the htaccess file pulls this_post and passes it as an argument to the index.html script. which is what the examples above should do. why it's changing the address bar to
mysite.com/blog/index.html?blog=index.html
i'm not sure why
side note, since i got this before...yes i'm aware of using the .php extention but .html is a client ask that they're not budging on.
edit-the link for possible duplicate was one I already found and tried to get the current RewriteRule but it's still not working.
The best way is to made something simple like this:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^([^/]*)\.html$ index.php?blog=$1 [L]
Options -MultiViews
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
then you can do URL like:
mysite.com/this_post
what is equal to:
mysite.com/index.php?blog=this_post
Like that you can do both calls and will work fine.
Generaly you need first decide how you want your SEO URL to look like and then you need to setup .htaccess regex and rules.
NOTE: you can't use .html for GET calls. You can use .php files for your works and use $_GET['blog'] to pickup your data from URL.
Seem to have found the combo that works, the [NC, L] flag combo keeps giving me a crash error so i left it just [NC]. Having it in the root account directory seems to not work also so i moved into the web directory (public_html) and that works for now.
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^blog/(.*) /blog/index.html?blog=$1 [NC]
Still not sure why the L flag gives me a server crash on the pages or why having it in the root doesn't work but moving it up into the public_html folder does, is it possibly a server configuration problem that i'm overlooking?

how to redirect to specific file by writing rule in .htaccess file?

I have developed a project without using any framework. I want to redirect a page to specific php file (example : movie/movie.php) when click on this
Click me
link. I have created .htaccess file and currently it look like this.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/movie/([0-9]+)/$ http://localhost/tthtml/movie/movie_review.php [L]
</IfModule>
I have enable mod_rewrite module in httpd.conf file. I am using wamp server in windows machine.
I dont no how to do this. I have referenced few sites and I created this above rule in my own. Kindly any one help me to do this and feel better if your answer have explonation as well. Thanks in advance.
Modify the rule as follows:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^movie/([0-9]+)/(.*)$ http://localhost/tthtml/movie/movie_review.php [L]
</IfModule>
The rule that you placed was not working because it was comparing only the string "/movie/1/", while you actually want to match "/movie/1/Baashha-Suresh-Krishna-1995".
For this to work, I have modified the RewriteRule directive as ^/movie/([0-9]+)/(.*)$ to accommodate the remaining string "Baashha-Suresh-Krishna-1995" or any other value that the application may have.

Flight PHP Routing from Subdirectory

So I'm using the Flight PHP microframework (http://flightphp.com/) to do routing. My question is, how can I run the router from within a subdirectory? What I mean is, essentially, run it 'sandboxed' within a folder.
As in, a request to '/' just pulls the regular index.php file. But a request to '/flight/file' would load the URL using Flight.
I know you can't just dump it in a folder on the server and expect it to work because FlightPHP expects the URLs relative to the root.
Is there a way to run FlightPHP isolated in a directory with the rest of the website running regular PHP?
EDIT
I tried simply putting the .htaccess file into the subdirectory. This has the peculiar effect of causing the routes to still act as if they are from the root (e.g. /thing/otherthing/ when it should be /otherdirectory/thing/otherthing/ ) while simultaneously causing the 404 callback to not work. Not what I intended.
EDIT 2
Contents of .htaccess file, which are what is suggested by the Flightphp website:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA,L]
I know this is an old question but I've been doing something similar just by adding something along the lines of
RewriteBase /flight/
in the .htaccess file (before all of your rules) of the flight directory. Hope this helps someone else looking for the same thing. Example:
RewriteEngine On
RewriteBase /flight/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA,L]
Sorry for giving you an answer that is not going to help a lot. I'm running Flight (also sandboxing) on a subdirectory. I've created an .htaccess just with the defaults in the subdir and Flight is now regarding this as it's root.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA,L]
Did you check the content of the .htaccess on the higher levels, maybe there is something that is blocking you

How can I display the resulting path when using mod_rewrite?

I am having a heck of a time trying to get an apache mod_rewrite rule to work. The thing that is making the process the most difficult is that I have no way of knowing what the final output string is. I continue to get 404 pages, but that doesn't tell me much except that I did something wrong. Is there some method of echoing out the final rewrite rather than redirecting me to a 404?
. . . And just in case someone feels like helping me out with my original problem, I'm trying to make it so that anyone who goes to /server/root/to/folder/public_html/ gets redirected to /server/root/to/folder/public_html/destinationFolder/. Below is one of the hundreds of variations that I've tried:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond $1 !^(favicon\.ico|assets)
RewriteRule ^public_html/(.*) destinationFolder/$1 [L]
</IfModule>
You are using system paths. Apache mod_rewrite only works with URLs (PT flag is implied in .htaccess files), that's your first error.
public_html is normally the root directory. For example: http://mydomain.com
Then, a complete rule set could be something like this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^(favicon\.ico|assets) [NC]
RewriteRule ^(.*)/?$ DestinationFolder/$1 [L]
Maps silently
http://mydomain.com/Anything
To:
http://mydomain.com/DestinationFolder/Anything
Except when anything is file favicon.ico or folder /assets
For permanent redirection, replace [L] with [R=301,L]
This is just an example. The rules and parameters have to be modified to meet the real requirements.

Convert PHP Variable URL with mod_rewrite to clean URL

I need to convert a link that contains php variables into a clean link that can be picked up and indexed by Google. I've looked at ways to do this, and using mod_rewrite apparently is the best way to handle this.
Currently my URLs look like this:
http://mysite.com/jobs/view/?j=senior-model-validator-groep-risk-management
I'd like to end up looking like this
http://mysite.com/jobs/view/senior-model-validator-groep-risk-management
It's probably really easy but sadly I know nothing about .htaccess. I tried a few things but I end up with nothing remotely close :-)
Thanks in advance!
Edit: Here's my full .htaccess file based on the help so far:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^jobs/view/(.*)$ jobs/view/?j=$1 [L]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
You may use an .htaccess file at the root of you web site with these rules:
RewriteEngine on
RewriteRule ^jobs/view/(.*)$ jobs/view/?j=$1 [L]
Do not forget to filter/validate GET input in the script dealing with the data...

Categories