I have a .htaccess file with the typical wordpress mod_rewrite:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
</IfModule>
# END WordPress
However I need to allow a certain page to not get redirected. that being 'domain/ee'
I tried adding:
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f
RewriteRule ^ %{DOCUMENT_ROOT}%{REQUEST_URI} [L]
to the top. As I read it, that should have checked to see if the page existed and if so not run the reset of the rewrite. However it's not working.
any ideas? thanks.
Make the first rewrite rule something like this:
RewriteRule ^(path/to/page)$ $1 [L]
These two rules should prevent rewriting any actual files or directories:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
Related
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.
I'm trying to create a vanity url for where users profile could easily be viewed by others.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [L]
RewriteRule ^(.*)$ http://localhost/test/public/profile.php?username=$1 [NC]
This is the code but each time i use this code in my .htaccess file, i get 500 Internal Server Error and the sub-folder public disappears from my directory. What am i getting wrong? Do i need any to do any configuration first?
It's better to use global rewrite logic:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]
and in Your index.php catch $_SERVER['REQUEST_URI'] and if user exists so respond if not then pass processing to next procedure.
But if You insist on realization as in question so try this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ profile.php?username=$1 [L,QSA]
Check this:
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^profile/([a-zA-Z0-9_\-/]+)$ test/public/profile.php?username=$1 [L,NC]
</IfModule>
However note:
You must have the rewrite engine on to make it work (google a bit for the installation procedure the OS/system you are working)
Put proper RewriteBase directive if you use it from subdirectory.
I'm using SimpleMVCFramework
All my routes are working fine, based on the default htaccess file:
Options -Indexes
<IfModule mod_rewrite.c>
Options -Indexes
RewriteEngine On
RewriteBase /mpl/servicos/smvcf/
# Force to exclude the trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.*)/$
RewriteRule ^(.+)/$ $1 [R=307,L]
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [QSA,L]
</IfModule>
I have a route that expects an id:
http://localhost/mpl/servicos/smvcf/detalhe/37343
But I need the URL the user sees to be friendly as:
http://localhost/mpl/servicos/smvcf/mercedes_benz-a-a_220_cdi_auto-37343.html
I thought something like this would work, but I get a 404:
RewriteRule ([^/]+)-([^/]+)-([^/]+)-([^/]+).html detalhe/$4
Please help.
The user sees the pretty url, but you are not interested in what the seo-title is. What you are interested in is the number that is hidden in it. As the number is in the very back of the pretty url, let's just match on that:
RewriteRule ^[^/]+-([0-9]+)\.html detalhe/$1 [L]
If you are trying to use another rewrite that will not be routed through the default MVC route index.php then you need to make sure you place your new rewrite before those rules. Also that folder you are redirecting to will need to have a index PHP file to do something as well.
Options -Indexes
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /mpl/servicos/smvcf/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)-([0-9]+)\.html detalhe/$2 [L]
# Force to exclude the trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.*)/$
RewriteRule ^(.+)/$ $1 [R=307,L]
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [QSA,L]
</IfModule>
I am having a hard time getting two rewrite rules to work. Right now these links do work:
http://domainname.com/blog/something/something/andsomethingelse/
http://domainname.com/blog/
http://domainname.com/leaderboard/
If I access, /leaderboard/something, it shows Not Found error page from WordPress blog, ignoring the leaderboard rewrite rule.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /leaderboard/index.php [L]
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
# Ignore real files/folders
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .? - [L]
RewriteRule ^blog/(.+?)$ /blog/index.php [L] # WP standard syntax
RewriteRule ^leaderboard/(.+?)$ /leaderboard/index.php?var=$1 [L]
You should write rewrite_rules like this code:
RewriteRule (.*)/(.*)/(.*)/(.*)$ index.php?var1=$1&var2=$2&var3=$3&var4=$4 [L]
Your writing code matches anything.
I believe this may be what you're looking for.
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^blog/(.+)$ /blog/index.php?path=$1
RewriteRule ^leaderboard/(.+)$ /leaderboard/index.php?path=$1
Then you can access the pages like so:
http://domain.com/blog/something/somethingelse
http://domain.com/leaderboard/something/somethingelse
and in the background it would look like this:
http://domain.com/blog/index.php?path=something/somethingelse
http://domain.com/leaderboard/index.php?path=something/somethingelse
I have created the folder http://domain.com/sport/
What I need it to do now is show contents of http://domain.com/sport/index.php whenever I access http://domain.com/sport/anything/anything/.
Right now its showing contents of http://livesports.pw/index.php
I tried code below, but it's not working.
RewriteEngine On
RewriteBase /
RewriteRule ^(.+)\/$ $1/ [R=301,L]
Note : The folder /sport/ is just an example folder. It can vary. Even if more folder are created, it
must work even for the new ones.
Replace your code with this:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d [OR]
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^ - [L]
RewriteRule ^([^/]+)/?$ /index.php [L]
RewriteRule ^([^/]+)/(.+?)/?$ /$1/index.php [L]