Redirect multiple slugs htaccess - php

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.

Related

In php how to remove the query string from URL and also filename the access the page?

I have a URL https://testing.in/show_tmp?tmp=abcval. I want a clean URL like https://testing.in/abcval without breaking the functionality of the site. My website is built in Core PHP. I know we can achieve this using the htacess file but i don't know how to do it please help me out.
htaccess file code
# DO NOT REMOVE THIS LINE AND THE LINES BELOW SSL_REDIRECT:connectwithme.in
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^connectwithme.in$
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# DO NOT REMOVE THIS LINE AND THE LINES ABOVE SSL_REDIRECT:connectwithme.in
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^.]+)$ $1.php [NC,L]
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/?show_tmp$ [NC]
RewriteRule /?(.+) show_tmp?tmp=$1 [NC,L,QSA]
We add a rewrite condition as to current request URI should not contain show_tmp to avoid too may redirects issue. Later, we just capture the request URI and redirect to show_tmp
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/?show_tmp$ [NC]
RewriteRule /?(.+) show_tmp?tmp=$1 [NC,L,QSA,P]
Demo: https://htaccess.madewithlove.be?share=0ddbbf44-57e1-5c9b-9e83-500961e8050d
After assisting with Anydesk access, your final .htaccess should look like below:
# DO NOT REMOVE THIS LINE AND THE LINES BELOW SSL_REDIRECT:connectwithme.in
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^connectwithme.in$
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# DO NOT REMOVE THIS LINE AND THE LINES ABOVE SSL_REDIRECT:connectwithme.in
#RewriteCond %{REQUEST_FILENAME} !-d
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_URI} !^/?assets/.+$ [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php !-f #add this for any kind of file extension. If you want to display .html, then you will have to add .html also
RewriteCond %{REQUEST_URI} !^/?assets/.+$ [NC]
RewriteCond %{REQUEST_URI} !^/?show_tmp$ [NC]
RewriteRule (.*) https://%{HTTP_HOST}/show_tmp?tmp=$1 [NC,L,QSA,P]
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^.]+)$ $1.php [NC,L]

RewriteRule get error when i call xajax

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]

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