.htaccess Mod Rewrite 'pretty ur'l to actual doesn't work - php

I'm trying to get htaccess to rewrite this url :
http://127.0.0.1/papercut3/article/post-title/
to this:
http://127.0.0.1/papercut3/article.php?p=post-title/
My current .htaccess is:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^article/(\d+)*$ ./article.php?p=$1
RewriteRule ^section/(\d+)*$ ./section.php?s=$1
And my urls written in the code:
http://127.0.0.1/papercut3/article/execution-call-for-cinema-killer/
My problem is that when I click the link I get this:
Not Found
The requested URL /papercut3/article/syria-suffers-deadliest-month/ was not found on this server.
I'm using WAMP, the .htaccess is in the root of the /papercut3 directory with all the core files. Any help would be greatly appreciated!:)

If you can use .htaccess files, you want it to look like:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^article/(.+)$ ./article.php?p=$1
RewriteRule ^section/(.+)$ ./section.php?s=$1
Or, you can be slightly more specific with what is allowed, but the one in your question only allows character [0-9], this way it opens it up and let's the respective PHP files do the checking/error-handling.

You may find help in the log file of apache, but I think this is related to AllowOverride directive, which must be set to None.
As said in the documentation, you need AllowOverride to contain at least FileInfo.
Edit: By the way, your rule won't match any of your url since you expect a int \d but don't provide any in input.

Related

Godaddy question mark and variable name hide php get

I have been trying for the past few days to get a Godaddy hosted server to hide the question mark and the variable name in the url automatically (ie. mysite.com/?id=5 >>> mysite.com/5) using Rewrite Rules in the .htaccess file.
My current .htaccess file looks like this:
RewriteEngine On
Options -MultiViews
RewriteBase /
RewriteRule ^/(.*)$ /index.php?id=$1 [P]
Am I doing something wrong or is it the Godaddy servers? Thank you in advance!
Try adding the following into your .htaccess file.
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^index/(\d+)*$ ./index.php?id=$1
You can learn more about clean urls here: http://www.desiquintans.com/cleanurls

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.

RewriteRule not working

I am trying to create friendly urls with .htaccess. I have checked and if I put deny from all on my .htaccess it does not load the webpage proving that the .htaccess is working.
I also have verified that mod-rewrite module is loaded on php using phpinfo
The problem is that when I access the url http://128.199.62.16/web/grupo/asd/123 it say not found and when i try with http://128.199.62.16/web/grupo/index.php?id=asd&description=123 it works as expected
Here you can find my .htaccess file content
RewriteEngine on
RewriteRule ^web/grupo/([^/]*)/([^/]*)/$ web/grupo/index.php?id=$1&descripcion=$2
Options -Indexes
ServerSignature Off
Can you please help me :D
Create a new file /var/www/html/web/grupo/.htaccess if it doesn't exist with this rule:
RewriteEngine on
RewriteBase /web/grupo/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?id=$1&descripcion=$2 [L,QSA]
Your input URL does not have a trailing slash, but your rule expects one.
I suggest putting a question mark after the trailing slash to indicate that it is optional.
Also, I suggest using + instead of * to indicate "one or more" rather than "zero or more".
RewriteRule ^web/grupo/([^/]+)/([^/]+)/?$ web/grupo/index.php?id=$1&descripcion=$2
For further reference, see:
trailing slash optional

Routing from slug url using htaccess

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

Remove php file extension from the url

I know this question has been asked like a 1000 times and I have probably tried like a 1000 suggestions as well, still no go.
I would like to remove the php file extension and substitute it with a slash (the slash is not so important but to remove the extension is).
My project is located here:
localhost/~fn/MyProject/
It contains two folders, public and includes. So all the public files are in the public folder: localhost/~fn/MyProject/public/index.php
I have tried so many suggestions already but most of them simply don't work. I am getting either a Internal Server Error, Forbidden or 404. I am putting the .htaccess to the public folder. Mod rewrite is on. No success with anything on stackoverflow and neither external resources ( e.g. http://alexcican.com/post/how-to-remove-php-html-htm-extensions-with-htaccess/ ). For example using the rewrite rules from the metioned webpage shows me 403 Forbidden to even access the index.
Any hints of what I may be doing wrong? I am really stuck. Thanks a lot!
If your htaccess is in public project folder, try with this code
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteCond %{THE_REQUEST} ^.*public/(.+)\.php
RewriteRule ^(.*)$ /~fn/MyProject/public/%1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ /~fn/MyProject/public/$1.php [L]
If the directory has an Index.php in it, the url will not show the file name when you browse to that folder. example.com/index.php would just show as example.com. You could use PHP includes to pull in each page to the index page in order to hide the file names but this isn't the best practice in general.
Put this in a .htaccess file in localhost/~fn/MyProject/ (so the file will be localhost/~fn/MyProject/.htaccess):
RewriteRule ^(.+?)\.php$ $1/ [QSA,L]
This captures anything ending in .php, strips off the .php, adds a /, and appends any query string as needed.
If you want only remove extension ".php" from index.php you should use mode_rewrite
RewriteRule ^index.php$ index // or
RewriteRule ^index.php$ /
But best way is implements mechanism to rewrite all urls and manage in your front script (index.php);
If you want use mode_Rewrite you shuold check if your mod_Rewrite is enabled on your Apache server. Go to apache.conf and check if line
LoadModule rewrite_module modules/mod_rewrite.so
is uncomented, dont forgot restart apache!
than you can On rewrite rule type in your apache.conf
<IfModule mod_rewrite>
RewriteEngine On
</IfModule>
or include something like in .htacces
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* index.php?%{QUERY_STRING} [L]
Now all signs from urls will going to your once index.php. Here you can do with this everything what do you need to do for example u can call some controller.
If you get 403 Forbiden dont forgot check chmod of the file.
To remove the .php extension from a PHP file for example yoursite.com/demo.php to yoursite.com/demo, you can add the following code to the .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

Categories