Redirect every link in php application - php

I have made a php web application which uses links like :
Link
... onClick="window.location.href='/asset/image.ext'" ...
<? header("Location: /login"); ?>
etc.
As you might have noticed, almost EVERY link starts with /.
Many links are rewritten in .htaccess like this :
Options All -Indexes
RewriteEngine on
RewriteRule ^login/error_418$ assets/common/login.php?&error [L]
RewriteRule ^login$ assets/common/login.php [L]
RewriteRule ^settings/change-profile-picture$ index.php?page=settings&command=profilePicture [L]
RewriteRule ^settings/change-password/error-requirements$ index.php?page=settings&command=password&error=requirements [L]
RewriteRule ^settings/change-password/error-missmatch$ index.php?page=settings&command=password&error=missmatch [L]
RewriteRule ^settings/change-password/error-418$ index.php?page=settings&command=password&error=418 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)/grades$ index.php?page=grades&course=$1 [L]
Now I need to move it on a web server where it will NOT be in the root direcotry but in a folder...
How do I now redirect all the root links to that folder ?

First place your existing rule in /folder/.htaccess like this:
Options All -Indexes
RewriteEngine on
RewriteBase /folder/
RewriteRule ^login/error_418$ assets/common/login.php?&error [L]
RewriteRule ^login$ assets/common/login.php [L]
RewriteRule ^settings/change-profile-picture$ index.php?page=settings&command=profilePicture [L]
RewriteRule ^settings/change-password/error-requirements$ index.php?page=settings&command=password&error=requirements [L]
RewriteRule ^settings/change-password/error-missmatch$ index.php?page=settings&command=password&error=missmatch [L]
RewriteRule ^settings/change-password/error-418$ index.php?page=settings&command=password&error=418 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)/grades$ index.php?page=grades&course=$1 [L]
Then create a rule in root .htaccess as this:
RewriteEngine On
RewriteBase /
RewriteRule ^((?!folder/).*)$ folder/$1 [L,NC]

Related

htaccess is not redirecting me on index.php page or default page

When I try to go on http://localhost/themobilesapp then it still redirect me on specification.php page not redirecting me on index.php
Now, I'm going on my homepage by typing http://localhost/themobilesapp/index.php
This is my full htaccess code.
Options +FollowSymLinks -MultiViews -Indexes
<IfModule mod_rewrite.c>
DirectoryIndex index.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
ErrorDocument 404 404error.php
RewriteRule ^([A-Za-z0-9-]+)?$ specification.php?url=$1 [L]
RewriteRule ^news/([A-Za-z0-9-]+)?$ news.php?url=$1 [L]
</IfModule>
How to re-direct it on index.php by typing http://localhost/themobilesapp ?
Try below rule like this,
DirectoryIndex index.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
ErrorDocument 404 404error.php
RewriteRule ^([A-Za-z0-9-]+)?$ specification.php?url=$1 [L]
RewriteRule ^news/([A-Za-z0-9-]+)?$ news.php?url=$1 [L]
RewriteRule ^$ /themobilesapp/index.php [R=301,L]
I don't see the index.php in your htaccess
I think you mean something like:
RewriteRule ^(.*)$ index.php [L]
This would mean other redirects below this rewrite won't get executed. Order matters; Use L flags where required.
Your regex:
RewriteRule ^([A-Za-z0-9-]+)?$ specification.php?url=$1 [L]
Matches everything (even an empty string; as in your example). If you leave the last ? out; the other string won't be optional:
RewriteRule ^([A-Za-z0-9-]+)$ specification.php?url=$1 [L]

http access for only 1 domain name

so i have loads of domain names on 1 hosting plan but have a main one in my root i am using http acess
options -multiviews
options All -Indexes
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^home$ index.php
RewriteRule ^dashboard$ dashboard.php
RewriteRule ^advertiser$ advertiser.php
RewriteRule ^add_campaign$ add_campaign.php
RewriteRule ^edit_campaign$ edit_campaign.php
RewriteRule ^stats$ view_statistics.php
RewriteRule ^login$ login.php
RewriteRule ^logout$ logout.php
RewriteRule ^withdrawals$ withdrawals.php
RewriteRule ^register$ register.php
RewriteRule ^report$ report.php
RewriteRule ^referrals$ referrals.php
RewriteRule ^contact$ contact.php
RewriteRule ^add_wallet$ add_wallet.php
RewriteRule ^payment_page$ payment_page.php
RewriteRule ^forgot_password$ forgot_password.php
RewriteRule ^my_account$ my_account.php
RewriteRule ^(admin)($|/) - [L]
RewriteRule ^([A-Za-z0-9-]+)/?$ view_link.php?s=$1 [L]
RewriteRule ^page_([^/]+)/?$ pages.php?slug=$1 [L]
</IfModule>
The rest of my domain names will now not work because it will not let me access any of the files by doing domainname.com/folder is there any way to set these rules just for the main domain name??
Try to add RewriteCode that matches the main domain name. Like this
options -multiviews
options All -Indexes
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.yourmaindomain.com$
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^home$ index.php
RewriteRule ^dashboard$ dashboard.php
RewriteRule ^advertiser$ advertiser.php
RewriteRule ^add_campaign$ add_campaign.php
RewriteRule ^edit_campaign$ edit_campaign.php
RewriteRule ^stats$ view_statistics.php
RewriteRule ^login$ login.php
RewriteRule ^logout$ logout.php
RewriteRule ^withdrawals$ withdrawals.php
RewriteRule ^register$ register.php
RewriteRule ^report$ report.php
RewriteRule ^referrals$ referrals.php
RewriteRule ^contact$ contact.php
RewriteRule ^add_wallet$ add_wallet.php
RewriteRule ^payment_page$ payment_page.php
RewriteRule ^forgot_password$ forgot_password.php
RewriteRule ^my_account$ my_account.php
RewriteRule ^(admin)($|/) - [L]
RewriteRule ^([A-Za-z0-9-]+)/?$ view_link.php?s=$1 [L]
RewriteRule ^page_([^/]+)/?$ pages.php?slug=$1 [L]
</IfModule>
Then the rewrite rules should only run if the domain matches.

Rewriting a url with more than one page

I'm trying to rewrite my urls on a page that has a pagination option and I'm unsure of what string to use because the one i'm currently using isn't working.
My document structure is below
blog-listing.php
htaccess
/blog/post.php
/css/
/js/
/images/
What's already in my htaccess file
RewriteEngine on
RewriteRule ^blog/category/([a-zA-Z0-9-/]+)$ /blog/archive.php?cat=$1 [L]
RewriteRule ^blog/category/([a-zA-Z0-9-/]+)/page/([0-9]+)/{0,1}$ /blog/archive.php?cat=$1&page=$2 [L]
RewriteRule ^blog/([a-zA-Z0-9-/]+)$ /blog/post.php?s=$1 [L]
RewriteRule ^blog/([a-zA-Z0-9-/]+)$ /blog/post.php?s=$1&page=$2 [L]
RewriteRule ^(css)($|/) - [L]
RewriteRule ^(perch)($|/) - [L]
RewriteRule ^(images)($|/) - [L]
RewriteRule ^(js)($|/) - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
The line i've tried to use
RewriteRule ^blog-listing/([a-zA-Z0-9-/]+)$ /blog-listing.php?s=$1&page=$2 [L]
The url I'm receiving
http://mysite(dot)com/blog-listing.php?page=2
The url I'd like
http://mysite(dot)com/blog-listing/page-2
Any ideas?
Use a simple file to manage your url
RewriteEngine On
# Redirect all requests not pointing at an actual file to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]
https://github.com/ogallardop/blackjack-api/blob/master/src/.htaccess
The framework you're using to pagination will do the rest.

Friendly URL within a directory

I have a Wordpress installation in a domain and within a directory, I have a site developed in PHP+MySQL completely independent of the wordpress. It works alone. This site is a backoffice, so it's installed under the /backoffice/ directory.
One of the directories of the backoffice I have another directory I want it works with friendly URL's.
In my .htaccess I have the following rules:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^backoffice/edit_user/([^/\.]+)/?$ /backoffice/edit_user/index.php?o=$1 [L,NC,QSA]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_URI} !=/server-status
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteRule ^login/?$ wp-login.php [QSA,L]
RewriteRule ^postpass/?$ wp-login.php?action=postpass [QSA,L]
RewriteRule ^logout/?$ wp-login.php?action=logout [QSA,L]
RewriteRule ^lostpassword/?$ wp-login.php?action=lostpassword [QSA,L]
RewriteRule ^retrievepassword/?$ wp-login.php?action=retrievepassword [QSA,L]
RewriteRule ^resetpass/?$ wp-login.php?action=resetpass [QSA,L]
RewriteRule ^rp/?$ wp-login.php?action=rp [QSA,L]
RewriteRule ^register/?$ wp-login.php?action=register [QSA,L]
When invoked a url like this one: mydomain.com/backoffice/edit_user/joseantonio the 404 page come up.
What's wrong?
Thanks in advance.
As it is in this answer - you can debug your Apache rewrites. Turn on RewriteLog in your virtual host OR server configuration:
RewriteEngine On
RewriteLog "/var/log/apache2/rewrite.log"
RewriteLogLevel 3
Then you'll see what rule makes it to redirect 404.
Finally, I could find the solution:
Here is the code used in conjuntion with the wordpress:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
**RewriteRule ^backoffice/edit_user/([^/\.]+)/?$ /backoffice/edit_user/index.php?o=$1 [L,NC,QSA]**
RewriteCond %{REQUEST_URI} !=/server-status
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteRule ^login/?$ wp-login.php [QSA,L]
RewriteRule ^postpass/?$ wp-login.php?action=postpass [QSA,L]
RewriteRule ^logout/?$ wp-login.php?action=logout [QSA,L]
RewriteRule ^lostpassword/?$ wp-login.php?action=lostpassword [QSA,L]
RewriteRule ^retrievepassword/?$ wp-login.php?action=retrievepassword [QSA,L]
RewriteRule ^resetpass/?$ wp-login.php?action=resetpass [QSA,L]
RewriteRule ^rp/?$ wp-login.php?action=rp [QSA,L]
RewriteRule ^register/?$ wp-login.php?action=register [QSA,L]
As you can see, I changed the position of my rule after the index.php rule, works fine, in any other position, it will not work.
You can try to use own .htaccess in /backoffice/ folder with
RewriteBase /backoffice/
# all the backofice rules

htaccess URL Rewrite Not working with first rule

I have the following rules setup for my blog and they seem to work just fine.
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/(index\.php|(modules|css|files|fonts|ico|img|js)/)
RewriteRule ^([^/]*)/page/([^/]*)$ /framework/?p=$1&page=$2 [L]
RewriteRule ^([^/]*)/search/([^/]*)$ /framework/?p=$1&search=$2 [L]
RewriteRule ^([^/]*)/search/([^/]*)/page/([^/]*)$ /framework/?p=$1&search=$2&page=$3 [L]
So I was feeling confident and wanted to add one more rule for the just the basic page.
so I added this rule
RewriteRule ^([^/]*)$ /framework/?p=$1 [L]
so that my htaccess file now looks like this
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/(index\.php|(modules|css|files|fonts|ico|img|js)/)
RewriteRule ^([^/]*)$ /framework/?p=$1 [L]
RewriteRule ^([^/]*)/page/([^/]*)$ /framework/?p=$1&page=$2 [L]
RewriteRule ^([^/]*)/search/([^/]*)$ /framework/?p=$1&search=$2 [L]
RewriteRule ^([^/]*)/search/([^/]*)/page/([^/]*)$ /framework/?p=$1&search=$2&page=$3 [L]
but for if I go to http://www.example.com/framework/blog I get a 500 Internal Server Error if I take out the line and go to http://www.example.com/framework/blog/page/2 it loads my second page without any issues.
I am not sure what I am doing wrong? Any advice would be appreciated.
You can use:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /framework/
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^([^/]+)/page/([^/]*)$ ?p=$1&page=$2 [L,QSA]
RewriteRule ^([^/]+)/search/([^/]*)/page/([^/]*)$ ?p=$1&search=$2&page=$3 [L,QSA]
RewriteRule ^([^/]+)/search/([^/]*)$ ?p=$1&search=$2 [L,QSA]
RewriteRule ^([^/]+)/?$ ?p=$1 [L,QSA]

Categories