How to redirect url in htaccess - .htaccess

How can I redirect below in htaccess
url/api/login?status_check=1 -> `url/stack/api/login.php?status_check=1`
url/u/123456789 -> `url/stack/user/show_profile.php?uid=123456789`

Sorry guys I post here because comments dont provide ability of posting codes. If anyone feels offended, I apologize.
I now get my url redirect working on local environment such as http://localhost/api/signup but it displays an error of 404 when I uploaded to the server as http://server.com/api/signup
<Files ~ "^\.(htaccess|htpasswd)$">
deny from all
</Files>
Options -Indexes
Options -Indexes
Options -Indexes
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^api/signup stack/api/api.signup.php [NC,QSA]
RewriteRule ^api/signin stack/api/api.signin.php [NC,QSA]
RewriteCond %{THE_REQUEST} ^.*/index\.php
RewriteRule ^(.*)index.php$ /$1 [R=301,L]

Related

Htaccess redirect to controller action don't work properly

I want to redirect to my contoller - SiteController.php and action actionLanguage. I think my htaccess is OK but can't reach the action. Everything that I am trying to put in the url leads me to admin/site/index and not to admin/site/language in my particular case. My .htaccess :
# prevent directory listings
Options -Indexes
# follow symbolic links
Options FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/admin/gii/?$
RewriteRule . backend/web/index.php?r=gii [L]
RewriteCond %{REQUEST_URI} ^/admin
RewriteRule ^admin(/.+)?$ /backend/web/$1 [L,PT]
RewriteCond %{REQUEST_URI} ^.*$
RewriteRule ^(.*)$ /frontend/web/$1
This line RewriteRule ^admin(/.+)?$ /backend/web/$1 [L,PT] must execute to backend/web/site/language when I type www.website.com/admin/site/languageright ? Or I am in some mistake? It redirects the request to the admin/site/index now. Appreciate every advice! Thank you in advance!
EDIT:
Backend .htaccess:
AddDefaultCharset utf-8
Options FollowSymLinks
IndexIgnore */*
RewriteEngine on
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php
RewriteRule ^static - [L]

ErrorDocument 404 not working with routing

I tried using this code on .htacces to have my own custom 404 on my page,.
ErrorDocument 404 http://mysite.com/404.shtml
when i visit the page that does not exist on the site. it must show something like this
but this is the one it shows
here is my complete htaccess
<files .htaccess>
order allow,deny
deny from all
</files>
php_value memory_limit 170M
RewriteEngine on
Options +FollowSymlinks
Options -Indexes
RewriteCond %{REQUEST_URI} (.+)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
AddDefaultCharset utf-8
RewriteRule (.*) index.php
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.mysite.com.*$ [NC]
RewriteRule ^(.*)$ http://mysite.com%{REQUEST_URI} [L,R=301]
ErrorDocument 404 http://mysite.com/404.shtml
The location of my htaccess is located along with the 404.shtml and the index.php of the site
Removing this code would fix the problem. How can i make it work with routing.
RewriteCond %{REQUEST_URI} (.+)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
AddDefaultCharset utf-8
RewriteRule (.*) index.php
Instead of referencing the entire url with ErrorDocument 404 http://mysite.com/404.shtml, name the document relative to your web root. In this case, /404.shtml.
Use these codes in your .htaccess for capture 404 error pages.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /404.html [L]
</IfModule>
Now your all 404 requests will goto /404.html. You can also use /404.php to code more dynamically. Also make sure that rewrite module is enabled in your apache.

Trouble with .htaccess mod_rewrite

Please can somebody take a look at my .htaccess code to help identify what's wrong.
I am trying to amend all example.php, example.html and example.htm URL's to index.php?filename=example.html
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(index\.php) - [L]
RewriteRule ^([^/\.]+).(php|html|htm)$ index.php?filename=$1.html&%{QUERY_STRING} [L]
</IfModule>
When I enter a URL like example.html I'm taken to the default 404 and not index.php
Try:
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?filename=$1 [L,QSA]
</IfModule>
I'm not using the apache .hatacces for a while,
But try this one:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/index\.php$
RewriteCond %{REQUEST_URI} !^.*/(css|img|js)($|/.*$) [NC]
RewriteRule ^(.*)$ /index.php?filename=$1&%{QUERY_STRING} [L]
Basically what you need is to be sure that the URL didn't have already index.php. You accomplish it with RewriteCond.
After you redirect all to index.php passing the filename as a parameter.
You can add more exception to the RewriteCond like on this example, all content with /css, /js, /img will not pass to this rule.
You can check this thread: Using .htaccess to reroute all requests through index.php EXCEPT a certain set of requests also

How to fix a mod_rewrite RewriteRule in HTACCESS

I'm writing a few RewriteRules, and my most basic rewrite returns a 404 error. Here's my code:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule s/(.*)/(.*)/$ /page.php?s=$2 [NC,L]
RewriteRule ^submit/$ submit.php [R,L]
# Options All -Indexes
<files .htaccess>
order allow,deny
deny from all
</files>
RewriteRule ^submit/$ submit.php [R,L] is where I'm having trouble.
When I visit domain.com/submit/, my server returns a 404 error saying, "The requested URL /submit/ was not found on this server." It's like the server did not even look at my HTACCESS file. The other RewriteRules work perfectly.
Am I missing something?
You may try this:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !page\.php [NC]
RewriteRule ^s/(.*)/(.*)/$ /page.php?s=$2 [NC,L]
RewriteCond %{REQUEST_URI} !submit\.php [NC]
RewriteRule ^submit submit.php [R,L,NC]
That's it according to the information in the question.
Can't guess what's the first rule for as there are no URL examples to determine the pattern to be matched by the regex or the location of the script.
Something similar happens with the second rule because there is no way to confirm where the script (submit.php) is located as, again, there are no URL examples in the question.

rewriting to always use www in domain ( even on subpages )

I've added the following to my .htaccess to properly redirect any visitors from http://domain.com to http://www.domain.com ... but I still can't get it to work with subpages...ie http://domain.com/subpage is not yet redirecting to http://www.domain.com/subpage .
Here is what I have in my htaccess now:
<Files wp-config.php>
order allow,deny
deny from all
</Files>
<Files .htaccess>
order allow,deny
deny from all
</Files>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteCond %{HTTP_HOST} !^www
RewriteRule (.*) http://www.%{HTTP_HOST}/$1 [L,R]
</IfModule>
So the question is what else would I need to add or update in my htaccess to get this working?
Many thanks for your suggestions!
You have the redirect happening after you've done routing to index.php, it needs to happen before any routing rules get executed. Try swapping them around:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www [NC]
RewriteRule (.*) http://www.%{HTTP_HOST}/$1 [L,R]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
(also added the [NC] flag so case isn't checked in hostname match)

Resources