Apache and Url Rewrite - php

I have 2 main pages. It works like so,
<html>
<body>
<div id='menu'></div>
<div id='content'><?PHP require("content.php"); ?></div>
</body>
</html>
Then, content.php pulls the "page" from the url, and checks it through a url of "good pages". If not, there is a default. Anyway, the typical url looks like this
/index.php?page=user&id=1
/?page=group&id=3
I'm trying to make it look like this
/user/1
/group/3
This is what I have for my .htaccess file, which doesn't work 100%. It pulls the correct page, but the css files doesn't seem to be uploaded. It is in /css/ in the main directory as index and content.
EDIT
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule ^(.*)/(.*)$ /index.php?page=$1&id=$2 [L]
I'd like the application to be scale-able if possible.
Thanks!

You will want to be using either an absolute path to your CSS, or use a base href for it, or you can prefix the path to the CSS with a / if the path to starts from the root domain.
Your .htaccess is fine, but might want some slight tweak:
RewriteEngine On
RewriteRule ^(\w)/([0-9]+)/$ /index.php?page=$1&id=$2 [L,QSA]

please try with the following code you can get the values in the $_GET['page']. Then you can split the values by / and then can get your user name,id etc
RewriteEngine On
RewriteBase /project/
RewriteRule ^([A-Za-z0-9]+){0,1}$ index.php?page=%{REQUEST_URI} [L,NC,QSA]
In the above code you may want to replace the /project/ with /. It will work perfectly

Try this .htaccess code
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^(.*)/(.*)$ /index.php?page=$1&id=$2 [L]
You can now directly access your webpage via..
www.yourdomain.com/user/1
www.yourdomain.com/group/3

Please try below code in .htaccess
RewriteEngine On
RewriteRule ^user/([0-9]+)/$ /index.php?page=user&id=$2 [L,QSA]
RewriteRule ^group/([0-9]+)/$ /index.php?page=group&id=$2 [L,QSA]

Related

Rewrite Rule for subfolder instead of main website

I have a htaccess code that i want to be changed.
RewriteEngine on
RewriteBase /
RewriteRule ^(.*)$ proxy.php?url=$1 [L,QSA]
It works like a charm if i place proxy.php and .htaccess file in main website and visit pages through www.domain.com/
I want to change this RewriteRule in a way that if I place the proxy.php and htaccess in subfolder named "folder" then new results show in www.domain.com/folder/
So mainly purpose is to change results path from www.domain.com to www.domain.com/folder/
You can place your code without any changes in /folder/.htaccess with proxy.php.
Or you can use:
RewriteEngine on
RewriteBase /
RewriteRule ^folder/(.+)$ folder/proxy.php?url=$1 [L,QSA]

it is possible to add folder prefix in for all images path?

I've a drupal 8 website and it contents contains some url like this
http://localhost/sites/default/files/inline-images/magento.png
this link is come from a block which was saved in db /sites/default/files/inline-images/magento.png
I want to convert all images into http://localhost/folder/sites/default/files/inline-images/magento.png
can i do this by .htaccess mod rewrite?
Did you try something like this:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^(.*)\.(png|jpg|gif|svg)$ /folder/$1.$2 [L,R=301]
</IfModule>
You can try removing this part R=301 and leave only L.
At the top of your htaccess root add the following :
RewriteEngine on
RewriteRule ^/?folder/sites/default/files/inline-images/(.+\.png)$ /sites/default/files/inline-images/$1 [L,NC]
This will internally redirect /folder/sites/default/files/inline-images/foo.png to /sites/default/files/inline-images/foo.png .

how to rewrite single URL using .htaccess

I need to rewrite only 1 specific URL, to display to visitors specific content: I tried something like, this:
RewriteEngine on
RewriteCond %{REQUEST_URI} example.com/test/2_5/page.html
RewriteRule ^(.*)$ example.com/tt.html [R,L]
I need to rewrite all requests to:
http://example.com/test/2_5/page.html
to
http://example.com/tt.html
how to do this?
thanks,
Redirect /test/2_5/page.html /tt.html
Ok, mod_rewrite
RewriteRule ^/test/2_5/page.html /tt.html [L]
Remove first / if using .htaccess in the site's root folder, not the .conf file. And, typically, the final url should be the full one, with http:// and domain, but this one will work too. If you want to do everything by the rules then
RewriteRule ^/test/2_5/page\.html$ http://example.com/tt.html [L]

How to get variable in query string without using "?"

I'm trying to use GET to capture member IDs in a URL without using the ?name=variable. For instance, instead of using mydomain.com/folder?id=1234 I would like to use mydomain.com/folder/1234 and pick up the id "1234" in the code so I can pass it to another URL.
This page simply redirects by using: <iframe src="http://redirect_domain.com/folder/index.php" />
I have no control over the server I'm redirecting to, but need the variable passed to it. I can append the redirect URL with ?id=1234 ie <iframe src="http://redirect_domain.com/folder/index.php?id=1234" />
Their code will pick up the id by using <?php echo $_GET["id"]; ?>
Thanks for your help.
You'll want to use .htaccess for this. Create a file called .htaccess and put it in the same folder mydomain.com is referencing. Put something like the following in there
RewriteEngine On
RewriteRule ^folder/(.*)$ folder?id=$1 [L]
Note: mod_rewrite must be enabled on apache, but it usually is.
Update: I've updated the .htaccess to reflect a local URL since you've made your desired functionality more clear. Using this .htaccess in conjunction with <iframe src="http://redirect_domain.com/folder/index.php?id=<?=$_GET['id']?>" /> should do the trick.
Create a .htaccess file and use something like this
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
I feel the need to write my own answer down. The following two lines in your .htaccess should do exactly what you are asking for:
RewriteEngine On
RewriteRule ^folder/(.*)$ redirect_domain.com/folder/index.php?id=$1 [L]
According to the rewrite rule test site http://martinmelin.se/rewrite-rule-tester/ , when I use an input of folder/1234, it is redirected to redirect_domain.com/folder/index.php?id=1234 which is exactly what you asked for. There is no need for the <iframe... or any actual files in the /folder/ other than the .htaccess ...

URL rewriting-page not found error

In order to convert my dynamic URL i.e www.3idiots.co.in/index.php to static url i.e www.3idiots.co.in/index.html, I edited my .htccess file and put the following code in it:
RewriteEngine On
RewriteRule ^index.php$ /index.html [R]
when i uploaded this file in the root directory,and try to open the page, I got the error
404 page not found error, www.3idiots.co.in/index.html not found.
You have to actually have a file named index.html. Right now you don't. The rewriting/redirecting is working fine, you're just redirecting to a non-existent page/file.
I'm a little confused as to what you're actually trying to do. If you just want to move index.php to index.html, rename the file. Rewriting makes it so that if someone tries to open index.php they will be redirected to index.html, but you still have to have an index.html file for them to be redirected to.
RewriteEngine On
# Send the user to index.html
RewriteRule ^index.php$ /index.html [R]
# Tell the server that index.html really means index.php
RewriteRule ^index.html$ /index.php
Try these rules:
RewriteCond %{THE_REQUEST} ^GET\ /index\.php
RewriteRule ^index\.php$ /index.html [L,R=301]
RewriteRule ^index\.html$ index.php [L]
The first rule redirects every direct request of /index.php externally to /index.html. And the second rule rewrites requests of /index.html internally to /index.php.
Are you sure mod rewrite is enabled & working?
1) create an html page called found.html with whatever you want in it, but some text to be sure it's loaded (not a blank page basically) and put this in an file called ".htaccess" :
RewriteEngine on
RewriteBase /
RewriteRule ^find.html$ /found.html [L]
2) upload both your .htaccess and the found.html files in your domain's root
3) Just try to load -www.example.com/find.html (with your real domain of course). If mod_rewrite is available, you should see the content of found.html while browsing find.html (which does not physically exist by the way).
If it does not work, try :
RewriteEngine on
RewriteBase /
RewriteRule ^find.html$ found.html [L]
In the Apache Conf file, you also need to make sure that AllowOverride is set to a value that will allow .htaccess to be processed.
Normally it's AllowOverride all
RewriteEngine On
RewriteRule ^index.html$ index.php
RewriteRule ^$index.htm/$ index.php [L]
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^index.html$ index.php
Try this code...
It will work for your problem /..
Best Of LUck
If it solve your problem..
Visit my site

Categories