How to hide everything after your webpage name with .htaccess - php

When I enter my site and when I browse other sites, I get a parameter more: etc .php?p=pagename&id=1
I would like to hide everything after .dk
I have tried:
RewriteEngine On
RewriteRule ^([a-zA-Z0-9]+)$ index.php?p=$1 [L,QSA]
RewriteRule ^([a-zA-Z0-9]+)/$ index.php?p=$1 [L,QSA]
RewriteRule ^/index.php$ http://www.madsweb.dk/ [R,NC,L]
But it doesn't seem to work.
Can anyone help me out?

RewriteRule ^/index.php$ http://www.madsweb.dk/ [R,NC,L]
Rewrites work the other way around. You don't rewrite what the user sees in his address bar but you rewrite a request to another request (like a redirect, but handled transparently on the server).
That's also the reason that what you try to do there is not possible.
You could use a frame instead if you like the 90s and don't care about usability.
But jokes aside, it's the best to build upon this rule, which is already good:
RewriteRule ^([a-zA-Z0-9]+)$ index.php?p=$1 [L,QSA]
and get the additional information (id in this case) into the pattern. Example:
RewriteRule ^([a-zA-Z0-9]+)/?$ index.php?p=$1 [L,QSA]
RewriteRule ^([a-zA-Z0-9]+)/([0-9]+)$ index.php?p=$1&id=$2 [L,QSA]
or more general (but still only one parameter):
RewriteRule ^([a-zA-Z0-9]+)/?$ index.php?p=$1 [L,QSA]
RewriteRule ^([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/([0-9]+)$ index.php?p=$1&$2=$3 [L,QSA]

I would like to hide everything after .dk
This is impossible. mod_rewrite cannot remove the need to have unique URLs for each resource.
(And if you did manage to do it, then it would break linking, including bookmarking and search engines).

Heeej
try
$pathInfo = explode('/', trim($_SERVER['PATH_INFO'], '/'));
$pagina = $pathInfo[0]; $actie = $pathInfo[1]; RewriteRule
^([a-zA-Z0-9]+)/?$ index.php?p=$1 [L,QSA] RewriteRule
^([a-zA-Z0-9]+)/([0-9]+)$ index.php?p=$1&id=$2 [L,QSA]
or more general (but still only one parameter):
RewriteRule ^([a-zA-Z0-9]+)/?$ index.php?p=$1 [L,QSA]
RewriteRule ^([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/([0-9]+)$ index.php?p=$1&$2=$3 [L,QSA]
or
list($pagina, $actie) = explode('/', trim($_SERVER['PATH_INFO'], '/'));
you also can try :
Options +FollowSymLinks RewriteEngine On
RewriteRule ^folder1/(.*)$
/Link.
com/folder2/$1 [R=301,L]

Related

.htaccess Pretty URLS issue

Having a bit of a problem and wondering what I've done wrong.
Options -Multiviews
RewriteEngine On
RewriteBase /
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.rentus.com/$1 [R=301,L]
RewriteRule ^([a-z]+)\/?$ $1.php [NC]
RewriteRule ^([-a-z]+)\/([-a-z]+)/?$ category.php?param=$1&param2=$2 [L,QSA]
RewriteRule ^([-a-z]+)\/([-a-z]+)/?$ city.php?param=$1&param2=$2 [L,QSA]
So it works with general pages like "/about/" and then category.php with query strings on category.php "/category/hats/".
When I try to load "/city/losangeles/" Its loading category.php file instead of city.php file. Any idea what I can do to make this work properly and use the php file I tell it too?
Your rules are the exact same, so it wont ever reach the second:
RewriteRule ^([-a-z]+)\/([-a-z]+)/?$ category.php?param=$1&param2=$2 [L,QSA]
RewriteRule ^([-a-z]+)\/([-a-z]+)/?$ city.php?param=$1&param2=$2 [L,QSA]
Try this (Be aware, your param will no longer contain category/city, and will now include the actual category or city):
RewriteRule ^category\/([-a-z]+)/?$ category.php?param=$1 [L,QSA]
RewriteRule ^city\/([-a-z]+)/?$ city.php?param=$1 [L,QSA]

.htaccess RewriteRule for category/page in custom CMS

I have a working RewriteRule that rewrites any page to the literal url:
RewriteRule ^(.+)$ index.php?page=$1 [QSA,L]
The problem is I want to add page=$1&category=$2 and convert it to...obviously... /category/page
I tried this, but it doesn't seem to work:
RewriteRule ^([^/]+)/([^/]+)$ index.php?page=$1&category=$2 [QSA,L]
I should note that I would still like to be able to access pages without categories - ie /login or /about that should go to index.php?page=about for instance
Your solution will be as below.
RewriteRule ^([0-9a-zA-Z_\-]+)/([0-9a-zA-Z_\-]+)$ index.php?page=$1&category=$2 [NC,L]
Input url : http://www.test.com/my-cat/my-page
and it will be treated as : http://www.test.com/index.php?page=my-cat&category=my-page
Try with below, we are instructing apache to don't look for directory or file.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)$ index.php?page=$1&category=$2 [QSA,L]
try this
#https://www.example.com/index.php?category=9&page=CBSE `#https://www.example.com/category/page/CBSE`
Method One:
#use this code for generate new url
RewriteRule ^index/([0-9a-zA-Z]+)/([0-9a-zA-Z]+)$ index.php?category=$1&page=$2 [NC,L]
Method Two :
RewriteRule ^index/([0-9a-zA-Z]+)/([0-9a-zA-Z]+)$ index.php?category=$1&page=$2
Note :Hit Your Url index.php?category=$1&page=$2 to convert $i & $2 Create Dynamic Url Your Id Bases
This will work for you.
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)$ /index.php?category=$1&page=$2 [L]
If you want to generate rewrite urls easily, there are a lot online rewrite url generators.
Thanks for all the contributions...
Because I needed to cater for both /page and /category/page...I ended up doing this:
RewriteRule ^([0-9a-zA-Z_\-][^/]+)$ index.php?page=$1 [N]
RewriteRule ^([0-9a-zA-Z_\-]+)/([0-9a-zA-Z_\-]+)$ index.php?category=$1&page=$2 [L]
But that was only half the solution as I needed to modify my PHP to check whether category is set or not and to send a 404 if it doesn't match. Otherwise /invalid_category/valid_pagewould still work.
The ideas above all pushed me in the right direction, thanks.

Difficult problems with mod_rewrite

I have a difficult problem:
In my .htaccess I have the following RewriteRules which do not function.
RewriteRule u/(.*)/ user.php?u=$1 [L,QSA]
RewriteRule u/(.*) user.php?u=$1 [L,QSA]
RewriteRule user/(.*)/ user.php?u=$1 [L,QSA]
RewriteRule user/(.*) user.php?u=$1 [L,QSA]
RewriteRule view/(.*)/ view.php?file=$1 [L,QSA]
RewriteRule view/(.*) view.php?file=$1 [L,QSA]
RewriteRule show/(.*)/ show.php?img=$1 [L,QSA]
RewriteRule show/(.*) show.php?img=$1 [L,QSA]
RewriteRule report/(.*) report.php?img=$1 [L,QSA]
RewriteRule report/(.*)/ report.php?img=$1 [L,QSA]
RewriteRule search/tag/(.*) search.php?t=$1 [L,QSA]
RewriteRule search/tag/(.*)/ search.php?t=$1 [L,QSA]
RewriteRule search/(.*) search.php?q=$1 [L,QSA]
RewriteRule search/(.*)/ search.php?q=$1 [L,QSA]
RewriteRule bug/view/(.*)/ bug.php?view=$1 [L,QSA]
RewriteRule bug/view/(.*) bug.php?view=$1 [L,QSA]
RewriteRule bug/(.*) bug.php?step=$1 [L,QSA]
RewriteRule bug/(.*)/ bug.php?step=$1 [L,QSA]
However, when I enter a RewriteRule as
RewriteRule ^ http://example.com [R, L]
I will be forwarded, so it works.
Solutions such as
RewriteRule u/(.*)/ http://example.com/user.php?u=$1 [L,QSA]
do not work. Also
RewriteRule /u/(.*)/ user.php?u=$1 [L,QSA]
or the like were not successful.
I have the problem since I recently moved to a new server. However, everything seems to be fine with the configuration.
Does anyone have an idea what I'm doing wrong?
Did you search for a possible answer?
I found this:
Why does this RewriteRule work with [R] but not with [QSA,L]?
It seems that:
The FastCGI version when paired with Apache 2.2 doesn't seem to like
the rewriterule index.php/$1. Instead it prefers index.php?$1 but some
CMS don't like that. The CMS I am using does not like that. So
undone's comment to use ?$1 was on the right track, but because I had
no explanation, and the CMS I am using (Open Journal Systems) does not
work with the pathinfo stuff after a ?, then it was just not working.
The solution was to change from FastCGI to just regular CGI.
check the other answers under that URL, they have valid points, that might help your case.
To me it seems like you are using a wrong syntax. Try to put your pattern in double quotes and see if it works (remember: the pattern in RewriteRule is a Regular Expression). Like this:
RewriteRule "u/(.*)/" "http://example.com/user.php?u=$1" [L,QSA]
Or a better way is to enclose your patterns in: ^ (as start of string) and $ (as end of string):
RewriteRule ^u/(.*)$ http://example.com/user.php?u=$1 [L,QSA]

How to write a 'root' RewriteRule redirecting to two different pages?

I am struggling with .htaccess, I have this code:
<pre>
# PRODUCTS
RewriteRule ^(.*)/(.*)$ /newwebsite/product.php?category=$1&product=$2 [L,NC]
# SERVICES
RewriteRule ^(.*)/(.*)$ /newwebsite/service.php?category=$1&service=$2 [L,NC]
# PLAIN PAGES
RewriteRule ^/$ /newwebsite/index.php [L,NC]
RewriteRule ^signup-free$ /newwebsite/signup-free.php [L,NC]
RewriteRule ^about$ /newwebsite/content.php?page=1 [L,NC]
RewriteRule ^portfolio$ /newwebsite/portfolio.php [L,NC]
RewriteRule ^our-stores$ /newwebsite/our-stores.php [L,NC]
RewriteRule ^contact$ /newwebsite/contact.php [L,NC]
RewriteRule ^products-list$ /newwebsite/lists.php?action=products [L,NC]
RewriteRule ^services-list$ /newwebsite/lists.php?action=services [L,NC]
</pre>
My code is not working properly, I need to add something to Products and Services rules to make them redirect to 'product.php' and 'service.php' respectively, without adding no more 'slash' or 'subfolder', but I don't know what and how, I mean I just need that:
http://www.root.com/maintenance-services/weekly-pool-service
redirects to =>
http://www.root.com/service.php?category=maintenance-services&service=weekly-pool-service
but at the same time I need that:
http://www.root.com/hot-tubs/jacuzzi
redirects to =>
http://www.root.com/product.php?category=hot-tubs&product=jacuzzi
How can I make a difference between PRODUCTS and SERVICES, how can I tell apache to go to different pages with the same parameters?
I am confused. Thank you very much.
Make file rewrite.php, redirect everything there:
RewriteRule ^(.*)/(.*)$ /newwebsite/rewrite.php?sub=$1&second=$2 [L,NC]
Put all logic in this file.
Pseudo code
if($_GET['sub'] exists in table "service") {
$_GET['category'] = $_GET['sub'];
$_GET['service'] = $_GET['second'];
include 'service.php';
}else{
...
}

htaccess With a different amount of variables

I have a website that on one specific page it requires an extra variable.
At the moment, the htaccess I am using is:
RewriteEngine On
RewriteRule ^([^/]*)/$ /index.php?page=$1 [L]
Which works fine with one variable.
How would I get it to work with :
http://tessite.com/index.php?page=test&id=1
So it looked like:
http://tessite.com/test/1
Thanks for any help or replies.
It can be coded like this:
RewriteRule ^([^/]*)/([^/]*)/$ index.php?page=$1&id=$2 [L]
RewriteEngine On
RewriteRule ^(.*)/(.*)/$ /index.php?page=$1&id=$2 [L]
RewriteRule ^(.*)/$ /index.php?page=$1 [L]
Just add $2!
Also, [^/] isn't required, you can simply use ..

Categories