RewriteRule get error when i call xajax - php

like a title i have a problem with this rewrite rule
I write this
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !\.(font|css|js|images|remote) [NC]
RewriteCond %{REQUEST_URI} !^/(font|css|js|images|remote/.*)$ [NC]
RewriteRule ^index$ /page/index [QSA,L]
RewriteRule ^(.*)/(.*)$ /page/article&category=$1&post=$2 [QSA,L]
When i call ajax remote from /remote/check-function.php from pages he load full /page/post with variable sent by ajax.
How i can resolve this issue
Thanks in advance.
Best.

The RewriteCond only applies to the RewriteRule that comes immediately. Properly that the url /remote/check-function.php matches the last rule and is rewritten. You should add RewriteCond to check whether the request matches the existing files / directories before applying the last rule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*)$ /page/article&category=$1&post=$2 [QSA,L]

Related

Redirect multiple slugs htaccess

My htaccess code is
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/newhome/hello-1/ [NC]
RewriteRule .* newhome/hello-2/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/newhome/hello-3/ [NC]
RewriteRule .* newhome/hello-4/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/newhome/hello-5/ [NC]
RewriteRule .* newhome/hello-6/ [R=301,L]
Here only first condition is working but the other conditions are not working
can any one pls help me out
thank you
One way to keep your rewrite logic clean is to use skip rules. The first rule says "skip the next three if the URI already maps to a file or a directory. If you've got no other rewrites to do in this case just replace the SKIP=3 by an END if using Apache 2.4+ and LAST otherwise.
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [SKIP=3]
RewriteRule ^newhome/hello-1/ newhome/hello-2/ [R=301,L]
RewriteRule ^newhome/hello-3/ newhome/hello-4/ [R=301,L]
RewriteRule ^newhome/hello-5/ newhome/hello-6/ [R=301,L]
Except that this may not do what you are intending. Consider the request Newhome/hello-3/something. This will match rule 3 so the URI will be replaced by the target newhome/hello-4/ -- that it the something bit will be lost. If you want to keep this content then try:
RewriteRule ^newhome/hello-1(/?.*) newhome/hello-2$1 [R=301,L]
etc.

htaccess - Redirect EVERYTHING through php file

RewriteEngine on
RewriteCond %{REQUEST_URI} !(\.|/$)
RewriteRule (.*) %{REQUEST_URI}/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ api.php?url=$1 [L]
Above shows my current configuration.
The problem with this is it only seems to be redirecting if a directory or file exists after the folder the htaccess file is in.
How can I rewrite this so it literally just redirects every single request to api.php with the full requested path? I'm not looking to see whether directories or files exist in the htaccess file, I can do that in api.php, I just want to push all the requests through that file.
You can use:
RewriteEngine on
RewriteCond %{REQUEST_URI} !(\.|/$)
RewriteRule (.*) %{REQUEST_URI}/ [R=301,L]
RewriteRule ^((?!api\.php$).*)$ api.php?url=$1 [L,QSA,NC]
Lookahead (?!api\.php$) means rewrite everything except /api.php to /api.php with query parameter url.
like this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^?]*)$ /api.php?path=$1 [NC,L,QSA]
make sure you have this before the route
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

mod_rewrite rules definition

i have a Problem with my .htaccess/mod_rewrite:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^sometest/(.+)/page/([0-9]+)$ index.php?main=sometest/$1&page=$2
RewriteRule ^(.+)$ index.php?main=$1 [QSA]
This does not work. If i visit my website the css files are not included.
I include them like "/assets/css/blabla.css".
If i delete just one RewriteRule (doesn't matter which one) it works. But i need both rules.
You must use skip flag skip|S=numfor this.
like :
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d [NC,OR]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteCond %{REQUEST_URI} ^(.*)/([^/]+)\.([^/]+)$
RewriteRule ^ - [S=2,L]
RewriteRule ^sometest/(.+)/page/([0-9]+)$ index.php?main=sometest/$1&page=$2 [S=1,L]
RewriteRule ^(.+)$ index.php?main=$1 [QSA,L]
see this page for more information http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriterule

Modification to a RewriteRule

The following RewriteRule works correctly
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://domain.com/$1/ [L,R=301]
until I place this one just right below
RewriteRule ^([a-z0-9]+)/$ /profile/company-profile.php?cname=$1 [NC,L]
Now every domain.com/something-here goes to the company-profile.php
How can I fix this ?
Firstly, the domain.com/login takes you to the index.php of the login folder.
When I type the second RewriteRule, if I write domain.com/login again it shows (in the background) the company-profile.php?cname=login
I would recommend to use this:
RewriteEngine On
RewriteBase /
# redirect company specific request
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z0-9\-_]+)/?$ profile/company-profile.php?cname=$1 [NC,L]
# redirect all others
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_URI} !/?$
RewriteRule ^(.*)$ http://domain.com/$1/ [L,R=301]

CodeIgniter Routing

I'm trying to build a short URI service with CI just so I can learn CI faster
anyway .. I got stuck at the routing
i hid the index.php then added the following route $route['([A-z0-9]{4})'] = "/forward/redirect/$1";
but it just shows my default controller
I also tried with HTaccess
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-z0-9]{4})$ /forward/redirect/$1 [NC]
It gives an error for not having any passed data
any help is appreciated.
Cheers
Since there is no physical path /forward/redirect/, you should redirect to the "catch all" index.php file in the root and input the path as parameter:
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-z0-9]{4})$ /index.php/forward/redirect/$1 [NC]
Or you can leave the rule as is and append another rule (this way you will have two rewriting cycles, the first one will rewrite to /forward/redirect/asdf and then the second one rewrites to index.php/forward/redirect/asdf:
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-z0-9]{4})$ /forward/redirect/$1 [NC]
RewriteRule ^(.*)$ index.php/$1 [QSA,L]
i finally got it working >_>
routes file contains this
$route['([A-z0-9]{4})'] = "/forward/redirect/$1";
htaccess contains this
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*) http://%1/$1 [R=301]
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-z0-9]{4})$ index.php/forward/redirect/?$1 [NC]
RewriteCond $1 !^(index\.php|images|robots\.txt|(.*).js|(.*).css|(.*).jpg|(.*).png)
//added (.*) so resources could be loaded properly :)
RewriteRule ^(.*)$ /index.php/$1 [L]

Categories