Small error when using .htaccess for clean urls - php

I am using .htaccess to manage clean URL's but for some reason I get a 404 Not Found Apache error in some cases.
If a user goes to http://domain.com/profile/ everything is fine.
If a user goes to http://domain.com/profile they will get the error
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)$ /index.php?page=$1&cmd=$2 [L]

Try this:
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
# /profile/cmd
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)$ /index.php?page=$1&cmd=$2 [L]
# /profile
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)$ /index.php?page=$1 [L]
</IfModule>

You can redirect the url with the url which has trailing slash using 301 redirect
Try the following along with your rewrite condition. This will redirect any url request to url with trailing slash. ex. http://domain.com/profile will be redirected to http://domain.com/profile/
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !example.php
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://domain.com/$1/ [L,R=301]

The rewrite rule you wrote requires one slash '/'.
Try this instead:
RewriteRule ^([^/]*)(/([^/]*))?$ /index.php?page=$1&cmd=$3 [L]

Related

Rewrite URL dont get values and error 404

My Htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /test
RewriteCond %{THE_REQUEST} \s/index\.php\?id=([0-9]+)&title=([^\s&]+)\s [NC]
RewriteRule ^ %2-%1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+?)-([0-9]+)$ index.php?id=$2&title=$1 [L]
</IfModule>
I have in php urls as this :
index.php?id=34234&title=Hello_World&name=Mike
In the link i put this :
TEST
And i try get this url
http://www.test.com/34234/hello_world/mike
But all time give error as 404 error and i don´t know which it´s the solution for this, Thank´s in advanced
Your original URL has 3 path segments while your rule accepts only two. You need to fix your regex patterns
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} \s/index\.php\?id=([0-9]+)&title=([^\s&]+)&name=([^\s&]+)\s [NC]
RewriteRule ^ %1/%2/%3? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([0-9]+)/([^/]+)/(.+)/?$ index.php.php?id=$1&title=$2&name=$3 [L]
</IfModule>

htaccess https://example.com/dash/something link redirect problem

I have some directories:
/
/dash
/something
I want to make htaccess to redirect all traffic to it's subfolder index.php , which i did, when i type it works
example.com/dash or example.com/something
But my problem is when i try something like this example.com/dash/login or example.com/dash/another
in this case i'm redirected to / directory
Here is my htaccess in / directory
https:// we.tl/t-NtTDlYKBHr
Options -Indexes -MultiViews +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(?!dash/)(.+)$ /index.php?u=$1 [NC,QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^dash/(.+)$ /dash/index.php?u=$1 [NC,QSA,L]
And in /dash
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?path=$1 [NC,L,QSA]
https:// we.tl/t-lhgQ03tTGE
I am not expert on this. but I think you achieve it by something like this.
put this file in your parent directory
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^none/ none/index.php [NC,L,QSA]
RewriteRule ^dash/ dash/index.php [NC,L,QSA]
RewriteRule ^something/ something/index.php [NC,L,QSA]
RewriteRule ^ index.php [NC,L,QSA]
also if you put htaccess in every directory you may use this in all directories.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [NC,L,QSA]

Trailing slash at end of URL wordpress

Dears,
I want to add / at the end of each url of site, below is what i found online, it works good, but in wp-admin it gives me wp-admin/index.php/index.php/. Like everytime i click on dashboard it will add /index.php/
code from htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1/ [L,R=301]
</IfModule>
Please advise.
Thanks
Keep redirect rule before your default WP rule:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI}/ [L,R=301,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
Make sure to use a new browser for your testing.

how to get rid of /index at end

I am using PHP codeigniter and chriskacerguis/codeigniter-restserver.
In log file I am seeing 404. because some how /index is getting append at end.
correct url is api/CurrentYear
DEBUG - 2016-02-02 02:14:48 --> UTF-8 Support Enabled
DEBUG - 2016-02-02 02:14:48 --> Global POST, GET and COOKIE data sanitized
ERROR - 2016-02-02 02:14:48 --> 404 Page Not Found: api/CurrentYear/index
Can anyone give me clue why /index is getting append at end or any solution.
my .htaccess look like this.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/(favicon\.ico|apple-touch-icon.*\.png)$ [NC]
RewriteRule (.+) index.php?p=$1 [QSA,L]
</IfModule>
If you want to remove index.php from URL then follow code will surely works for you.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
<IfModule mod_rewrite.c>
Please remove the index.php in codeIgniter Framework config file
$config['index_page'] = '';
And apply below htaccess coding
RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|css|images|robots\.txt)
RewriteRule .* index.php/$0 [PT,L]
This code works for me
Options +FollowSymLinks
RewriteEngine on
RewriteCond $1 !\.(gif|jpg|shtml|ico|swf|wav|mp3|less|cur)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php?p=$1 [QSA]

.htaccess rule appending index.php instead of URI

My .htaccess looks like this..
Options -Indexes
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
RewriteCond %{HTTP_HOST} ^sub\.domain\.com$ [NC]
RewriteRule ^(.*)$ https://www.domain.com/members/invite/$1 [R,L]
</IfModule>
I want to be able to redirect an URL that looks like this..
https://sub.domain.com/ZTHF76
to...
https://www.domain.com/members/invite/ZTHF76
But instead I am getting
https://www.domain.com/members/invite/index.php
I need to keep the first rule to remove index.php from the URI of the rest of the application.
Any help with this greatly appreciated.
You should reorder your rules and keep redirect rule before rewrite one:
Options -Indexes
RewriteEngine On
RewriteCond %{HTTP_HOST} ^sub\.domain\.com$ [NC]
RewriteRule ^(.*)$ https://www.domain.com/members/invite/$1 [L,R=302]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

Categories