Rewrite rules html/php - php

I currently have this .htaccess code to remove .php/.html extensions.
# Apache Rewrite Rules
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
# Add trailing slash to url
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/|#(.*))$
RewriteRule ^(.*)$ $1/ [R=301,L]
# Remove .php-extension from url
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^\.]+)/$ $1.php
# Remove .html-extension from url
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^([^\.]+)/$ $1.html
# End of Apache Rewrite Rules
</IfModule>
I am trying to have a page poll.php to be /poll/123123/ <- (6 digit ID) from poll.php?poll=123123.
I tried adding in
RewriteRule ^[A-Za-z0-9-]+/([A-Za-z0-9-]+)/?$ polls.php?poll=$1 [NC,L]
but it then tries to detect dashboard.php instead of dashboard.html.
Any help would be appreciated, thanks.

There is no need for a wildcard if you're looking for a sp[ecific URL and only the number changes
RewriteRule ^poll/(\d+)/$ polls.php?poll=$1
or better to get always 6 digits
RewriteRule ^poll/([0-9]{6})/$ polls.php?poll=$1
That should do

Related

Removed .php extension but now it says page does not exist

I was able to get the .php off my files but now when I go to the pages they do not exist(and it's forcing a WordPress 404 page when the target files are actually on the server outside of WordPress). I think the problem might be that it's on a server with WordPress and these files lie outside of word press. There's already rewrite rules for WordPress and maybe it's conflicting with my rewrite rules for other files?
.htaccess file:
# 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
# Remove .php extension on files outside of wordPress
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.php
RewriteRule (.*)\.php$ /$1/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/(.+)/$
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^(.*)/$ $1.php [L]
# Force trailing slash
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule .*[^/]$ $0/ [L,R=301]
UPDATE - Full .htaccess
# BEGIN WP Hide & Security Enhancer
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#WriteCheckString:1548706048_68906
RewriteRule .* - [E=HTTP_MOD_REWRITE:On]
RewriteRule ^site(.*) /wp-login.php$1 [L,QSA]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^wp-login.php /index.php?wph-throw-404 [L]
RewriteCond %{REQUEST_URI} /enter$
RewriteRule ^(.*)$ /enter/ [R=301,L]
RewriteRule ^enter(.*) /wp-admin$1 [L,QSA]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^wp-admin(.+) /index.php?wph-throw-404 [L]
</IfModule>
# END WP Hide & Security Enhancer
# 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
You need to do something like this
#remove all PHP extensions from the url
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.php
RewriteRule (.*)\.php$ /$1/ [L,R=301] #end and redirect
# Force trailing slash ( probably no longer needed, because `/$1/` )
#RewriteCond %{REQUEST_FILENAME}.php -f
#RewriteRule .*[^/]$ $0/ [L,R=301]
#add .php back in and continue.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/(.+)/$
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^(.*)/$ $1.php #no last flag as we can now continue to WP with our nice php extensions back on
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
I'm assuming those rewrite rules are correct. I would have to look them up I haven't worked heavily with Rewrites in a few years. Basically
first remove any php extensions and redirect (if they exist)
on a redirect (or a request without .php) add back the php extension to those urls where it was removed so you may need %{REQUEST_FILENAME} !-d just to be safe.
carry on as normal
Just as a general rule of thumb, it's best to do redirects first. That way you can get to the redirect with the shortest amount of work. A redirect is like changing the url and then putting that in the browser and running it again (ie. a brand new request).
Another way to do this is to redirect all requests to a certain folder to an index.php of your making that has a router.
Such as this one (that I made as an example)
https://github.com/ArtisticPhoenix/MISC/tree/master/Router
Then your URL's can be like this
http://yourdomain.com/somedir/index.php/controller/method/...args
#or without the index.php
http://yourdomain.com/somedir/controller/method/...args
Then in HTACCESS all you would do is remove index.php
#remove index.php and redirect
RewriteRule somedir/index.php/(.+)/?$ /somedir/$1/ [L,R=301] #end and
#add index.php back in
RewriteRule somedir/(.+)/?$ /somedir/index.php/$1/ [L]
//wordpress stuff
The nice thing about routers is you never have to touch Htaccess, and with a folder name to key in on it should make things simpler.
Using the router and the example files:
http://yourdomain.com/somedir/user/login
Should go to Controllers/user.php method login.

remove .php with .htaccess

I am creating a .htaccess file in the path of my site var/www/mysite/ to remove my files the ".php", but does not work
# Apache Rewrite Rules
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
# Add trailing slash to url
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/|#(.*))$
RewriteRule ^(.*)$ $1/ [R=301,L]
# Remove .php-extension from url
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^\.]+)/$ $1.php
# End of Apache Rewrite Rules
</IfModule>
It is the first time I do this and if I'm not doing well, or whether this in the right place, so I come to you.
Try this code in site root .htaccess:
Options +FollowSymLinks
RewriteEngine On
# Add trailing slash to url that is not a file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*?[^/])$ %{REQUEST_URI}/ [L,R=302,NE]
# Add .php extension from urls internally
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php

Remove extension and add get value with traling slash

To do this :
Original URL -> localhost/viewprofile.php
ReWrite URL -> localhost/viewprofile/
I used this Code.
# Apache Rewrite Rules
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
# Add trailing slash to url
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/|#(.*))$
RewriteRule ^(.*)$ $1/ [R=301,L]
# Remove .php-extension from url
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^\.]+)/$ $1.php
# End of Apache Rewrite Rules
</IfModule>
Now to do this I used
Original URL -> localhost/viewprofile.php?user_id = 1
ReWrite URL -> localhost/1/
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ viewprofile.php?user_id=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ viewprofile.php?user_id=$1
But Unfortunately I want to do this
Original URL -> localhost/viewprofile.php?user_id = 1
ReWrite URL -> localhost/viewprofile/1/
and this
Original URL -> localhost/viewprofile.php?username=sean
ReWrite URL -> localhost/viewprofile/sean/
I have those two Original URLs and i want to rewrite them as respective ReWrite URL respectively as given above.
Anyone any solutions?
You can match on digits (only) for the first one and on alphanumeric for the other one (for instance)
<IfModule mod_rewrite.c>
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
# Add trailing slash to url
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/|#(.*))$
RewriteRule ^(.+)$ $1/ [R=301,L]
# Remove .php-extension from url
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
RewriteRule ^(.+)/$ $1.php [L]
RewriteRule ^viewprofile/([0-9]+)/?$ viewprofile.php?user_id=$1 [L]
RewriteRule ^viewprofile/([A-Za-z0-9]+)/?$ viewprofile.php?username=$1 [L]
</IfModule>

Variable Rewrite for multiple .php pages

Im trying to access two of my pages and rewrite their urls with .htaccess.
First page I'm trying to access:
http://api.aotikbot.tv/polls.php?username=aotik
Second page I'm trying to access:
http://api.aotikbot.tv/getpoll.php?poll=917529
My current .htaccess code:
# Apache Rewrite Rules
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
# Add trailing slash to url
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/|#(.*))$
RewriteRule ^(.*)$ $1/ [R=301,L]
# Remove .php-extension from url
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^[A-Za-z0-9-]+/([A-Za-z0-9-]+)/?$ polls.php?username=$1 [NC,L]
RewriteRule ^[A-Za-z0-9-]+/([A-Za-z0-9-]+)/?$ getpoll.php?poll=$1 [NC,L]
RewriteRule ^([^\.]+)/$ $1.php
# Remove .html-extension from url
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^([^\.]+)/$ $1.html
# End of Apache Rewrite Rules
</IfModule>
The first page works as /polls/username but the second doesn't read the poll value and I think redirects to polls.php.
Could anyone explain what I'm doing wrong?
Thanks in advance
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^[A-Za-z0-9-]+/([0-9-]+)/?$ getpoll.php?poll=$1 [NC,L]
RewriteRule ^[A-Za-z0-9-]+/([A-Za-z0-9-]+)/?$ polls.php?username=$1 [NC,L]
Here, I've swapped your two rules around, so we check for IDs first (for getpoll - note how I have removed letters from the expression). If we're not doing an ID check (ie, there are letters in the segment), then it'll rewrite for polls.
Update:
Per arco444, if the username is a number, this wouldn't work. This is a more specific solution:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^getpoll/([0-9-]+)/?$ getpoll.php?poll=$1 [NC,L]
RewriteRule ^polls/([A-Za-z0-9-]+)/?$ polls.php?username=$1 [NC,L]

Remove /index.php/ part of CodeIgniter URLs

I tried to get rid of "index.php" in url using the following rewrite code.but it's not working
please help me to fix this bug
# Development
RewriteEngine On
RewriteBase /mvcTestApp/blog/ciBlog/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|images|scripts|styles|vendor|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
In .htaccess file, add the following.
#Rewrite index.php
#Start using rewrite engine
RewriteEngine On
#Rewrite condition
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#Whenever index.php is there in the url, it will rewrite to / automatically
RewriteRule .* index.php/$0 [PT,L]
Check this link for more detail: http://subhra.me/remove-index-php-urls-codeigniter/
This htaccess is working for me.Try this.
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]
Use this rule
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
missing "/" before "index.php" in last rule
or refer URL
http://ellislab.com/codeigniter/user-guide/general/urls.html
Here's my suite of rewrite rules for CodeIgniter. Please read the inline comments:
## Rewrite rules
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# Remove the "index.php" part of URI
# Remove access to "codeigniter" and "data" folders
RewriteCond %{REQUEST_URI} ^(codeigniter|data).*
RewriteRule ^(.*)$ /index.php?/$1 [L]
# Block access to "hidden" directories whose names begin with
# a period. e.g. .git, .svn
RewriteCond %{SCRIPT_FILENAME} -d
RewriteCond %{SCRIPT_FILENAME} -f
RewriteRule "(^|/)\." - [F]
# WWW redirect
RewriteCond %{HTTP_HOST} ^(?!www\.).*
RewriteCond %{HTTP_HOST} ^(?!dev\.).*
RewriteRule (.*) http://www.%{HTTP_HOST}/$1 [R=301,L]
# Checks to see if the user is attempting to access a valid file,
# such as an image or css document, if this isn't true it sends the
# request to the root index.php.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>

Categories