htaccess allow only numbers after a certain point with https - php

i attached my current htaccess file below, it currently makes all my urls www with trailing slash at the end and if it is on and only on /prepaid/ it will be https.
i am tring to extend this functionality, where i can also accept requests from /prepaid/refill/a 10 digit phone number and have it be https as well
i started by adding this line at the bottom, but how would i extend the https part to allow from this path as well?
RewriteRule ^prepaid/refill/([0-9]+)/?$ index.php?p=prepaid&phone=$1 [L]
the current code
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ $1/ [L,R=301]
RewriteCond %{HTTPS} off
RewriteCond %{QUERY_STRING} !. [NC]
RewriteRule ^prepaid/?$ https://www.domain.com/prepaid/ [R=301,L]
RewriteCond %{HTTPS} on
RewriteRule ^prepaid/(.+) http://www.domain.com/prepaid/$1 [R=301,L,QSA]
RewriteRule ^prepaid/?$ index.php?p=prepaid [L]
RewriteRule ^prepaid/h2o-wireless/?$ index.php?p=prepaid&s=h2o [L]
RewriteRule ^prepaid/net10-wireless/?$ index.php?p=prepaid&s=net10 [L]
RewriteRule ^prepaid/page-plus-cellular/?$ index.php?p=prepaid&s=pageplus [L]
RewriteRule ^prepaid/red-pocket-mobile/?$ index.php?p=prepaid&s=redpocket [L]
RewriteRule ^prepaid/simple-mobile/?$ index.php?p=prepaid&s=simplemobile [L]

Have your full .htaccess like this:
RewriteEngine On
RewriteBase /
# force www
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# force trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301]
# force HTTPS for /prepaid/ OR /prepaid/refill/([0-9]+)/
RewriteCond %{HTTPS} off
RewriteCond %{QUERY_STRING} !.
RewriteRule ^prepaid(/refill/[0-9]+)?/?$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NC]
# force HTTP for everything else
RewriteCond %{HTTPS} on
RewriteRule ^prepaid/(?!refill/[0-9]+).+$ http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NC]
# internal rewrites
RewriteRule ^prepaid/?$ index.php?p=prepaid [L,QSA,NC]
RewriteRule ^prepaid/h2o-wireless/?$ index.php?p=prepaid&s=h2o [L,QSA,NC]
RewriteRule ^prepaid/net10-wireless/?$ index.php?p=prepaid&s=net10 [L,QSA,NC]
RewriteRule ^prepaid/page-plus-cellular/?$ index.php?p=prepaid&s=pageplus [L,QSA,NC]
RewriteRule ^prepaid/red-pocket-mobile/?$ index.php?p=prepaid&s=redpocket [L,QSA,NC]
RewriteRule ^prepaid/simple-mobile/?$ index.php?p=prepaid&s=simplemobile [L,QSA,NC]
RewriteRule ^prepaid/refill/([0-9]+)/?$ index.php?p=prepaid&phone=$1 [L,QSA,NC]

to use https need write some ike this
with regular expressions, you must be caution and be:
\d+ - its only numberic characters
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule ^prepaid/refill/(\d+)/?$ index.php?p=prepaid&phone=$1 [L]

Related

htaccess rewrite links and duplicate it

I have this htaccess code
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^einfogarden.com$
RewriteRule (.*) http://www.einfogarden.com/$1 [R=301,L]
RewriteCond %{THE_REQUEST} \s/+single\.php\?title=([^\s&]+) [NC]
RewriteRule ^ %1/? [R=302,L,NE]
## Adding a trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{THE_REQUEST} \s/+(.*?)[^/][?\s]
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=302]
# convert %20 to -
RewriteRule "^(\S*) +(\S* .*)$" $1-$2 [L,NE]
RewriteRule "^(\S*) (\S*)$" $1-$2 [L,R=302,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/?$ single.php?title=$1 [NE,L,QSA]
which should remove single.php?title= from url and replace the 20% by slash and it work corrctly.
but i have 2 problem 1. it stop the css in my website 2. if you try to click any link (except) the home page it will give you something like this http://www.einfogarden.com/%D9%81%D9%88%D8%A7%D8%A6%D8%AF-%D8%A7%D9%84%D8%AC%D8%B1%D8%AC%D9%8A%D8%B1/single.php?title=%D9%81%D9%88%D8%A7%D8%A6%D8%AF%20%D8%A7%D9%84%D8%B1%D9%85%D8%A7%D9%86%20%D8%A7%D9%84%D8%B1%D8%A7%D8%A6%D8%B9%D8%A9
it duplicate the link
You are asking in the .htaccess the url of your current page.
So yourwebsite.com/home will be yourwebsite.com/home/home
I edited your code a little. using this will do fine.
RewriteCond %{REQUEST_FILENAME} -f [NC,OR]
RewriteCond %{REQUEST_FILENAME} -d [NC]
RewriteRule ^(.*?)$ $1 [L]
RewriteCond %{REQUEST_URI} !^/cache
RewriteCond %{REQUEST_URI} !^/images
RewriteCond %{REQUEST_URI} !.*\.(css|jpg|gif|zip|js)
RewriteRule ^(.*)/(.*)/?$ index.php?page1=$1&page2=$2 [L]
RewriteRule ^(.*) index.php?page1=$1 [L]
#RewriteRule ^(.*)/?$ http://www.google.com/ [R]
This is a easy way the use SEO url's
The RewriteRule is for creating a new var to your url.

rewriteCond request with htaccess

I would like to accomplish the followings (rewrite not redirect):
searchPage.php?crs_category=variable like searchPage.php?crs_category=business to category/business or category/business/ case in-sensitive
You can also directly access category/name which is the rewrite of searchPage
index.php/name to page/name case in-sesitive. note this is a rewrite not a redirect and i would like access the index.php/name just with a clean url.
You can also directly access page/name which is the rewrite of index.php/name
Note:
- it needs to take into account spaces such as if a user click on searchPage.php?crs_category=html%20and%css it would rewrite the url to category/html and css
Tried:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} /searchPage.php\?crs_category=([^\s]+) [NC]
RewriteRule ^category/%1? [NE,NC,R,L]
RewriteRule ^category/([^/]+)/?$ /searchPage.php?crs_category=$1 [QSA,L,NC]
RewriteCond %{THE_REQUEST} /index\.php/(\S+) [NC]
RewriteRule ^ /page/%1 [NE,R,L]
RewriteRule ^page/([^/]+)/?$ index.php/$1 [NC,L]
Problem:
For some reason the request doesn't work for these lines:
RewriteCond %{THE_REQUEST} /searchPage.php\?crs_category=([^\s]+) [NC]
RewriteRule ^category/%1? [NE,NC,R,L]
In addition, I am not sure who take into account white space. where if its searchPage.php?crs_category=html%20andcss it leads to category/html and css disregarding the spacing code
Update:
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteBase /
# keep replacing space to hyphen until there is no space use internal rewrite
RewriteRule ^([^\s%20]*)[\s%20]+(.*)$ $1-$2 [E=NOSPACE:1]
# when there is no space make an external redirection
RewriteCond %{ENV:NOSPACE} =1
RewriteRule ^([^\s%20]+)$ $1 [R=301,L]
RewriteCond %{THE_REQUEST} /searchPage\.php\?crs_category=([^&]+?)\s [NC]
RewriteRule ^ category/%1? [NE,R,L]
RewriteCond %{THE_REQUEST} /index\.php/(\S+) [NC]
RewriteRule ^ page/%1 [NE,R,L]
RewriteRule ^category/([^/]+)/?$ searchPage.php?crs_category=$1 [QSA,L,NC]
RewriteRule ^page/([^/]+)/?$ index.php/$1 [NC,L]
You should be matching / after index.php not ?:
You can use these rules:
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} /searchPage\.php\?crs_category=([^&]+?)\s [NC]
RewriteRule ^ category/%1? [NE,R,L]
RewriteCond %{THE_REQUEST} /index\.php/(\S+) [NC]
RewriteRule ^ page/%1 [NE,R,L]
# keep replacing space to hyphen until there is no space use internal rewrite
RewriteRule ^(\S*)\s+(.*)$ $1-$2 [E=NOSPACE:1,DPI]
# when there is no space make an external redirection
RewriteCond %{ENV:NOSPACE} =1
RewriteRule ^(\S+)$ $1 [NE,R=302,L]
RewriteRule ^category/([^/]+)/?$ searchPage.php?crs_category=$1 [QSA,L,NC]
RewriteRule ^page/([^/]+)/?$ index.php/$1 [NC,L]

Underscores to hyphens dynamic url

I have the following existing .htaccess in place
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteRule home index.php [L]
RewriteRule ^([^/]*)/$ /index.php?TopicID=$1 [L]
RewriteRule ^([^/]*)/([^/]*)/$ /index.php?RollID=$1&FrameID=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule (.*) $1.php [L]
RedirectMatch 301 /cache(.*) //$1
ErrorDocument 404 http://www.example.com/error.php
I have many dynamically generated URLs which are being rewritten to this
www.example.com/abc_def_ghi/ OR
www.example.com/abc_def_ghi/abc_def_ghi/
and i'd like to end up with this
www.example.com/abc-def-ghi/ OR
www.example.com/abc-def-ghi/abc-def-ghi/
I've tried adding this to the end of my .htaccess
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !.(html|php)($|?) [NC]
RewriteRule ^([^_]*)_+(.*)$ $1-$2 [E=underscores:Yes,N]
RewriteCond %{ENV:underscores} ^Yes$
RewriteRule (.*) http://%{HTTP_HOST}/$1 [R=301,L]
but i'm getting a 500 error.
Can anyone help?
Rearrange your rules like this:
ErrorDocument 404 http://www.example.com/error.php
RewriteEngine on
RewriteRule ^cache(.*) /$1 [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
# if there is only one underscore then repalce it by - and redirect
RewriteRule ^([^_]*)_([^_]*)$ /$1-$2 [L,R=302]
# if there are more than one underscores then "repeatedly" repalce it by - and set env var
RewriteRule ^([^_]*)_([^_]*)_(.*) $1-$2-$3 [E=USCORE:1]
# if USCORE env var is set and there is no underscore left then redirect
RewriteCond %{ENV:REDIRECT_USCORE} =1
RewriteRule ^([^_]+)$ /$1 [L,R=302]
RewriteRule home index.php [L]
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^([^/]+)/?$ /index.php?TopicID=$1 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)/?$ /index.php?RollID=$1&FrameID=$2 [L,QSA]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule (.*) $1.php [L]

Htaccess rewrite match same patterns

How can I make both rewrites work like http://example.com/something.html http://example.com/videos/something/1.html it always matches the download.php one but not the video.php.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteBase /
RewriteRule ^category/(.+)$ category.php?q=$1
RewriteRule ^videos/(.+)/(.+).html$ video.php?q=$1&page=$2
RewriteRule ^(.+).html$ download.php?id=$1
You can have these rules as:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L,NE]
# skip all files and directories from rewrite rules below
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^category/(.+)$ category.php?q=$1 [L,QSA,NC]
RewriteRule ^videos/([^/]+)/([^/.]+)\.html$ video.php?q=$1&page=$2 [L,QSA,NC]
RewriteRule ^(.+)\.html$ download.php?id=$1 [L,QSA,NC]
Try this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteBase /
RewriteRule ^category/(.+)$ category.php?q=$1
RewriteRule ^videos/(.+?)/(.+?).html$ video.php?q=$1&page=$2
RewriteRule ^(.+).html$ download.php?id=$1
+ is a greedy operator. This is why download.php was always matched.

stop .htaccess from redirecting subfolder/index.php to root

I have an issue I can't seem to debug. I have tried many solutions and no luck.
When I access a URL with subfolder/index.php I get redirected back to the root page. If I access another page in that subfolder it works fine. I am not sure what is causing this on my code. Here is what I have.
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^domain.com$
RewriteRule (.*) http://www.domain.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www.domain\.com
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
RewriteRule (.*)\.php$ $1 [R=301]
# remove index
RewriteRule (.*)index$ $1 [R=301]
# remove slash if not directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/ $1 [R=301]
# add .php to access file, but don't redirect
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
Any suggestions? Thanks!
Keep your rules like this:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^domain\.com$
RewriteRule (.*) http://www.%{HTTP_HOST}/$1 [R=301,L,NE]
RewriteCond %{HTTP_HOST} ^www.domain\.com
RewriteCond %{THE_REQUEST} \s/+(?:index)?(.*?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]
# remove slash if not directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule ^(.+?)/$ $1 [R=301,L,NE]
# add .php to access file, but don't redirect
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1.php [L]

Categories