htaccess need name in front of domain - php

So i have a file named page.php, which has 2 get variables. the u and edit so at the moment i have
mysite.com/page.php?u=dan &
mysite.com/page.php?u=dan&edit
But how can i make the url look like this using htaccess?
dan.mysite.com &
dan.mysite.com/edit
At the moment i have tried this
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ page.php?u=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ page.php?u=$1
but this just makes
mysite.com/dan

If I understand your requirement correctly, you can use:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$ page.php?u=dan&$1 [L,QSA]

Related

.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.

Rewrite - 2 files with same parameters and located in same folder

I know that was a frequent question here in SO, but I can't rewrite some dynamics URL.
Hope someone can help me.
I've 2 files located in the same folder, that receive same number of parameters:
.com/serie.php?id=__123__&nombre=__string__
.com/episodio.php?idSerie=__123__&query=__1x1__
My desired pretty url are this:
for serie.php: .com/__123__/string
for episodio.php: .com/__123__/string/1x1-some-other-strings-not-relevants-here
I was able to make the first rewrite, like this:
Options +FollowSymLinks
RewriteEngine on
# Rewrite Serie.php
RewriteRule ^([^/]*)/$ /serie.php?id=$1 [L]
RewriteRule ^([^/]*)/([^/]*)/$ /serie.php?id=$1&nombre=$2 [L]
Work fine, but the problem comes in the second rewrite.
The bold characters are "relevants" and I use this, to make some work.
I tried something like this:
RewriteRule ^([^/]*)-([^/]*)$ /episodio.php?idSerie=$1&query=$2 [L]
But I get 404 not found.
How can make this second rewrite?
Thanks in advice.
You can use:
Options +FollowSymLinks
RewriteEngine on
# skip all files and directories from rewrite rules below
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^([^/]+)/?$ serie.php?id=$1 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)/?$ serie.php?id=$1&nombre=$2 [L,QSA]
RewriteRule ^([^/]+)/[^/]+/([^-]+)-[^/]*/?$ serie.php?id=$1&query=$2 [L,QSA]

Create different URLs for different languages with htaccess

I am trying to create different URLs for different languages, for example http://localhost/shift/en without "en" being an actual folder (shift is an actual sub folder). I want the 'en' to be a variable I can use in my PHP code. Here is how my htaccess looks like:
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /shift/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ public/index.php [QSA,L]
RewriteRule ^(en|de|fr)/(.*)$ $2?lang=$1 [L,QSA]
RewriteRule ^(.*)$ $1?lang=en [L,QSA]
Here's my PHP code:
var_dump($_GET['lang']);
When I go to http://localhost/shift/en it gives me a 404 and a PHP error: "undefined index: lang".
You need to edit your .htaccess like this-
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?lang=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?lang=$1
and the index.php:
echo $_GET['lang'];
If the url is- http://example.com/en, then the output will be- en
You can do the other things then, with the language.
If you want to give different versions of the page, I recommend you using a page for each language and include them if a language is asked for in the url.
For example- If someone types http://example.com/en, in the index.php you need to do this-
if($_GET['lang']=="en") {
$_SESSION['language']="en"; //Adding a session makes your life easier
include("en.php");
//everything else
}
I have updated the rewrite rule. Check it out.

mod_rewrite: form URL with multiple parameters to a shorter one (without middleparts)

Right now I work with these type of URLs:
http://subdomain.example.com/index.php?page=contact
http://subdomain.example.com/index.php?page=about
http://subdomain.example.com/index.php?page=category&category_id=1
http://subdomain.example.com/index.php?page=item&item_id=1
And I want them to look like this:
http://subdomain.example.com/contact/
http://subdomain.example.com/about/
http://subdomain.example.com/category-1/
http://subdomain.example.com/item-1/
So, I accommodate my page/php file into the following:
category_id -> category_name
item_id -> item_name
And my .htaccess file to this:
RewriteEngine on
RewriteRule ^([^/]*)/$ /index.php?page=$1 [L]
RewriteRule ^([^/]*)/([^/]*)/$ /index.php?page=$1&category_name=$2 [L]
Which gives me some nice URLs but I want them without the middlepart. In other words I want to get rid of "category" and "item" for example.
http://subdomain.example.com/category/category-1/ -> http://subdomain.example.com/category-1/
When I try and change my .htaccess to the following I get this to work. BUT you can see that I had to comment out the 2nd line and so my contact page doens't work any longer :(
RewriteEngine on
#RewriteRule ^([^/]*)/$ /index.php?page=$1 [L]
RewriteRule ^([^/]*)/$ /index.php?page=category&category_name=$1 [L]
I know it has something to do with the parameters, one against two but I can't figure out which way I have to follow to get this to work which makes me to have fit.
I really would appreaciate if somebody can help me out solving this!
If I understand...
RewriteEngine on
# Not for real file or directory
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
# for category-1/ and all like xxxxx-xxx-nnn
RewriteRule ^([^/]+-\d+)/?$ /index.php?page=category&category_name=$1 [L]
# for contact/ or about/ or other/
RewriteRule ^([^/]+)/$ /index.php?page=$1 [L]
The result :
http://subdomain.example.com/lord-of-the-rings-1/
-> http://subdomain.example.com/index.php?page=category&category_name=lord-of-the-rings-1

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